If you have a small amount of data, then you can use SharedPreferences. Mostly, SharedPreferences use for the user’s basic information, for example, id, name, email, and access token, etc.
SharedPreference provides modes for storing the data(Private and public mode). MODE_PRIVATE keeps the data private and secure. MODE_PUBLIC will make the data public which could be accessible by other applications on the device.
In this example, you’re going to learn login flow using SharedPreferences. First, look at the demo video.
1- Open the Android Studio, create a new project, and select a blank template
2- Make sure, add the below dependencies in the build.gradle.xml file and sync the Gradle.
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
3- Open the colors.xml file and add the below code. You can keep it by your requirements.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#121212</color>
<color name="purple_700">#121111</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<!-- App colors -->
<color name="color_bg">#121212</color>
<color name="color_sub_title">#CCCACA</color>
<color name="color_gradient_start_color">#B50931</color>
<color name="color_gradient_end_color">#671776</color>
</resources>
4- Open the strings.xml file and the below code.
<resources>
<string name="app_name">SharedPreferencesExample</string>
<!-- App Strings-->
<string name="str_pro_code_guru">ProCodeGuru</string>
<string name="str_login">Login</string>
<string name="str_sign_up">Sign Up</string>
<string name="str_login_sub_title">Please login to your account</string>
<string name="str_create_a_account">Create a new account</string>
<string name="str_hint_username">Enter username</string>
<string name="str_hint_pwd">Enter password</string>
<string name="str_hint_new_pwd">Enter new password</string>
<string name="str_hint_confirm_pwd">Enter confirm password</string>
<string name="str_register_now">Register Now</string>
<string name="str_web_address">procodeguru.com</string>
<string name="str_username"><![CDATA[<b>Hi <font color=#FF0000>%s</font></b>]]></string>
<string name="str_welcome_msg">There\'s new course about android development</string>
<string name="str_welcome__detail_msg">Find out how our new matching tool can help you learn another way</string>
<string name="str_logout">Logout</string>
</resources>
4- Right-click on the package and create a new Java interface called AppPreferences.java. We will define methods that will use to access the data.
package com.example.sharedpreferencesexample;
public interface AppPreferences {
String KEY_USERNAME = "key_username";
String KEY_PASSWORD = "key_password";
String KEY_IS_LOGGED_IN = "key_is_logged_in";
void saveUserInfo(String username, String password,boolean isLoggedIn);
String getUserName();
String getPwd();
boolean isLoggedIn();
void logout();
}
5- Create a new Java class called UserAppPreferenceHelper.java and implement AppPreferences.java.
package com.example.sharedpreferencesexample;
import android.content.Context;
import android.content.SharedPreferences;
public class UserAppPreferenceHelper implements AppPreferences {
private final SharedPreferences sharedPreferences;
UserAppPreferenceHelper(Context context) {
sharedPreferences = context.getSharedPreferences("user_app_helper", Context.MODE_PRIVATE);
}
@Override
public void saveUserInfo(String username, String password, boolean isLoggedIn) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_USERNAME, username);
editor.putString(KEY_PASSWORD, password);
editor.putBoolean(KEY_IS_LOGGED_IN, isLoggedIn);
editor.apply();
}
@Override
public String getUserName() {
return sharedPreferences.getString(KEY_USERNAME, null);
}
@Override
public String getPwd() {
return sharedPreferences.getString(KEY_PASSWORD, null);
}
@Override
public boolean isLoggedIn() {
return sharedPreferences.getBoolean(KEY_IS_LOGGED_IN, false);
}
@Override
public void logout() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(KEY_IS_LOGGED_IN, false);
editor.apply();
}
}
6- Now create an Application class that will act as a controller of the project. In this controller, we will create the instance of SharedPreference class and will access it on the Splash and Login screen.
package com.example.sharedpreferencesexample;
import android.app.Application;
public class AppController extends Application {
private static AppController mInstance;
public UserAppPreferenceHelper userAppPreferenceHelper;
public static synchronized AppController getInstance() {
return mInstance;
}
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
userAppPreferenceHelper = new UserAppPreferenceHelper(getApplicationContext());
}
}
7- Create a new layout resource file called activity_splash.xml.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_bg">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
app:layout_constraintBottom_toTopOf="@+id/tvHeading"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_pro_code_guru"
android:textColor="@color/white"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgLogo" />
<TextView
android:id="@+id/tvWebAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_web_address"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/leftGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/rightGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
8- Create a new Java class called ActivitySplash.java. In the ActivitySplash.java file, we will check if the user already logged in then we will redirect to Home Screen else on Login Screen.
package com.example.sharedpreferencesexample;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(() -> makeDecision(), 3000);
}
/**
* this method will call after 3 second and will make the decision of next screen
*/
private void makeDecision() {
if (AppController.getInstance().userAppPreferenceHelper.isLoggedIn())
openMainActivity();
else
openLoginActivity();
}
/**
* if user is already logged in then this dashboard or main screen will open
*/
private void openMainActivity() {
startActivity(new Intent(this, MainActivity.class));
finish();
}
/**
* if user is not logged in then login screen will open
*/
private void openLoginActivity() {
startActivity(new Intent(this, LoginActivity.class));
finish();
}
}
9- Navigate to res=>layout=>right-click and create a new resource file called activity_login.xml.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_bg">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="211dp"
android:layout_height="187dp"
android:layout_marginTop="40dp"
android:src="@drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_pro_code_guru"
android:textColor="@color/white"
android:textSize="35sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgLogo" />
<TextView
android:id="@+id/tvLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_login"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvHeading" />
<TextView
android:id="@+id/tvSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_login_sub_title"
android:textColor="@color/color_sub_title"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvLogin" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputUsername"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvSubTitle">
<EditText
android:id="@+id/edtUserName"
android:layout_width="match_parent"
android:inputType="text"
android:imeOptions="actionNext"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/bg_edt_field"
android:hint="@string/str_hint_username" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputPwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/inputUsername"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/edtPwd"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edt_field"
android:hint="@string/str_hint_pwd" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/btn_bg_gradient"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_login"
android:onClick="validateLoginInfo"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/inputPwd" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnRegisterNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_medium"
android:text="@string/str_register_now"
android:background="@android:color/transparent"
android:textAllCaps="false"
android:onClick="openRegisterActivity"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintTop_toBottomOf="@+id/btnLogin" />
<TextView
android:id="@+id/tvWebAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_web_address"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/leftGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/rightGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
10- Create a new Java class called LoginActivity.java. Add the below code
package com.example.sharedpreferencesexample;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
private EditText edtUserName;
private EditText edtPwd;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initComponents();
}
/**
* init the components
*/
private void initComponents() {
edtUserName = findViewById(R.id.edtUserName);
edtPwd = findViewById(R.id.edtPwd);
}
/**
* this will call from xml when user will click on the register now button
* @param view button view
*/
public void openRegisterActivity(View view) {
startActivity(new Intent(this, RegisterActivity.class));
}
/**
* this method will call when all user logged in successfully
*/
private void openHomeActivity(){
startActivity(new Intent(this, MainActivity.class));
finish();
}
/**
* this method will call from xml and validate the all required information
* @param view button view
*/
public void validateLoginInfo(View view) {
if (edtUserName.getText().toString().trim().isEmpty()) {
Toast.makeText(this, "Username field required!", Toast.LENGTH_LONG).show();
return;
}
if(edtUserName.getText().toString().trim().length()<5){
Toast.makeText(this, "Username length should be greater than 4", Toast.LENGTH_LONG).show();
return;
}
if (edtPwd.getText().toString().trim().isEmpty()) {
Toast.makeText(this, "Password field required!", Toast.LENGTH_LONG).show();
return;
}
if(edtPwd.getText().toString().trim().length()<5){
Toast.makeText(this, "Password length should be greater than 4", Toast.LENGTH_LONG).show();
return;
}
if(AppController.getInstance().userAppPreferenceHelper.getUserName() == null){
Toast.makeText(this, "Account does not exist", Toast.LENGTH_LONG).show();
return;
}
if(AppController.getInstance().userAppPreferenceHelper.getPwd() == null){
Toast.makeText(this, "Account does not exist", Toast.LENGTH_LONG).show();
return;
}
if(edtUserName.getText().toString().trim().equals(AppController.getInstance().userAppPreferenceHelper.getUserName())
&& edtPwd.getText().toString().trim().equals(AppController.getInstance().userAppPreferenceHelper.getPwd())){
AppController.getInstance().userAppPreferenceHelper.saveUserInfo(edtUserName.getText().toString().trim(),edtPwd.getText().toString().trim(),true);
Toast.makeText(this, "Login Successful", Toast.LENGTH_LONG).show();
openHomeActivity();
}else{
Toast.makeText(this, "Username or password is invalid", Toast.LENGTH_LONG).show();
}
}
}
11- Navigate to res=>layout=>right-click and create a new resource file called activity_register.xml and add the below code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_bg">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="211dp"
android:layout_height="187dp"
android:layout_marginTop="40dp"
android:src="@drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_pro_code_guru"
android:textColor="@color/white"
android:textSize="35sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgLogo" />
<TextView
android:id="@+id/tvLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_sign_up"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvHeading" />
<TextView
android:id="@+id/tvSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_create_a_account"
android:textColor="@color/color_sub_title"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvLogin" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputUsername"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvSubTitle">
<EditText
android:id="@+id/edtUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="text"
android:layout_marginTop="10dp"
android:background="@drawable/bg_edt_field"
android:hint="@string/str_hint_username" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputPwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/inputUsername"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/edtPwd"
android:layout_width="match_parent"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:layout_height="wrap_content"
android:background="@drawable/bg_edt_field"
android:hint="@string/str_hint_new_pwd" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputCPwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/inputPwd"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/edtCPwd"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edt_field"
android:hint="@string/str_hint_confirm_pwd" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnRegisterNow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/btn_bg_gradient"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_register_now"
android:textColor="@color/white"
android:onClick="createAccount"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/inputCPwd" />
<TextView
android:id="@+id/tvWebAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:fontFamily="@font/roboto_regular"
android:text="@string/str_web_address"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/leftGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/rightGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
12- Create the new Java class called RegisterActivity.java and add the below code
package com.example.sharedpreferencesexample;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class RegisterActivity extends AppCompatActivity {
private EditText edtUserName;
private EditText edtPwd;
private EditText edtCPwd;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initComponents();
}
/**
* init the components
*/
void initComponents() {
edtUserName = findViewById(R.id.edtUserName);
edtPwd = findViewById(R.id.edtPwd);
edtCPwd = findViewById(R.id.edtCPwd);
}
/**
* this method will call from xml file and will create the new account after validate the required information
* @param view button view
*/
public void createAccount(View view) {
if (edtUserName.getText().toString().trim().isEmpty()) {
Toast.makeText(this, "Username field required!", Toast.LENGTH_LONG).show();
return;
}
if (edtUserName.getText().toString().trim().length() < 5) {
Toast.makeText(this, "Username length should be greater than 4", Toast.LENGTH_LONG).show();
return;
}
if (edtPwd.getText().toString().trim().isEmpty()) {
Toast.makeText(this, "Password field required!", Toast.LENGTH_LONG).show();
return;
}
if (edtPwd.getText().toString().trim().length() < 5) {
Toast.makeText(this, "Password length should be greater than 4", Toast.LENGTH_LONG).show();
return;
}
if (edtCPwd.getText().toString().trim().isEmpty()) {
Toast.makeText(this, "Confirm password field required!", Toast.LENGTH_LONG).show();
return;
}
if (edtCPwd.getText().toString().trim().length() < 5) {
Toast.makeText(this, "Confirm password length should be greater than 4", Toast.LENGTH_LONG).show();
return;
}
if (!edtPwd.getText().toString().trim().equals(edtCPwd.getText().toString().trim())) {
Toast.makeText(this, "Password does not match", Toast.LENGTH_LONG).show();
return;
}
AppController.getInstance().userAppPreferenceHelper.saveUserInfo(edtUserName.getText().toString().trim(), edtPwd.getText().toString().trim(), false);
Toast.makeText(this, "Account Successfully created", Toast.LENGTH_LONG).show();
finish();
}
}
13- Open the activity_main.xml file and add the below code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_bg">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:src="@drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/roboto_regular"
android:text="Hi Rana"
android:textColor="@color/white"
android:textSize="35sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgLogo" />
<TextView
android:id="@+id/tvWelcomeMsg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:fontFamily="@font/roboto_regular"
android:gravity="center"
android:text="@string/str_welcome_msg"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvHeading" />
<TextView
android:id="@+id/tvSubTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/roboto_regular"
android:gravity="center"
android:text="@string/str_welcome__detail_msg"
android:textColor="@color/color_sub_title"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvWelcomeMsg" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnRegisterNow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="@drawable/btn_bg_gradient"
android:fontFamily="@font/roboto_regular"
android:onClick="logout"
android:text="@string/str_logout"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/leftGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/rightGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
14- Open the MainActivity.java file and add the below code.
package com.example.sharedpreferencesexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvHeading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponents();
}
/**
* init the components
*/
private void initComponents() {
tvHeading = findViewById(R.id.tvHeading);
tvHeading.setText(Html.fromHtml(String.format(getResources().getString(R.string.str_username), AppController.getInstance().userAppPreferenceHelper.getUserName())));
}
/**
* set the login status false and will redirect to login screen
* @param view button view, this will call from xml file.
*/
public void logout(View view) {
AppController.getInstance().userAppPreferenceHelper.logout();
startActivity(new Intent(this, LoginActivity.class));
finish();
}
}
15- Download the source code
Thanks for reading the tutorial. Subscribe to my YouTube channel. Like and Share my Facebook page with your friends.