𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Greeting dear members of Hive Learners, I hope you all are doing well. A warm welcome to you in the 26th chapter on Android App Development. In the previous lecture, we complete the Sign in and Sign-up coding and successfully tested the app. In today's Lecture, we will design the Welcome screen with some buttons and we will open this screen on successful; login. We also need to store another value in shared preference to save the session for later use. So let's get started.
GitHub Link
Use this GitHub project to clone into your directory. It will constantly get updated in the following lecture so you will never miss the latest code. Happy Coding!.
What Should I Learn
- Design a welcome screen
- Store the session
Assignment
- Design a welcome screen and store the login session
Procedure
First of all, we need a new Empty Activity, Welcome_Activity
and add some required widgets in the design layout of this welcome activity.
Hhave ere I two textview a Welcome text and a textview to show the username of a login user.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Welcome_Activity"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textSize="28sp"
android:textStyle="bold"
android:layout_gravity="center" />
<TextView
android:id="@+id/welcome_username_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="14sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginTop="4dp"/>
</LinearLayout>
Now we add two card views wrapped in the 2 Liearlayouts. The first linear orientation is verticle and in it another linear layout with horizontal orientation. In this Linearlayuout e add two card views with weight=1 and in the card,,, view add the TextView. Here is the code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp"
tools:context=".Welcome_Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Welcome"
android:textSize="28sp"
android:textStyle="bold" />
<TextView
android:id="@+id/welcome_username_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:text=""
android:textSize="14sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
app:cardBackgroundColor="@color/purple_200"
app:cardCornerRadius="10dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Deposit"
android:textColor="@color/white" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
app:cardBackgroundColor="@color/teal_700"
app:cardCornerRadius="10dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Transfer"
android:textColor="@color/white" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Now we are ready with the design declare and initialize the username in the WelcomeActivity.java
file. Also, declare and initialize the sharedPreferences.
Now we need to add a value in sharedPreference that will tell us which account is login currently. We also use a logic in Main_Activity.java
that check is this value is empty or not. If not empty then it will redirect the user to welcome screen without login again.
sharedPreferences.edit().putString("login_account",email_et.getText().toString()).apply();
Here is the logic that we use in the Main_Activity.java on app start to check the session.
Now on welcome screen we need to set the user name in welcome_username_tv, we get it from the sharedPreferences. First we need to get the login user email by using login_account key from sharedPreferences then we use this email_name to get the username.
Let's run and check if the app is working or not.

Thank You
