𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Greeting to all the Hive Learners, Welcome to the 25th lecture on Android App development. I hope you are learning something new day by day. Today we will work on the signing button and show a login successful message or login failed accordingly. We will also add the check as we do in the 24th lecture. I forget to convert the email to lowercase in lecture 24 so I will add the toLowerCase() function to store and get the value from the sharedPreferences.
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
- Write logic for sign-in button
Assignment
- Create a successful signing form
Procedure
First I add the same SharedPreferences as we do in lecture 24. Declare and initialize the sharedPreferences.
Then we need to add some checks to the signing button click listener.
if (TextUtils.isEmpty(email_et.getText().toString())){
email_et.setError("Invalid Email");
return;
}
if (TextUtils.isEmpty(pass_et.getText().toString())){
pass_et.setError("Invalid Password");
return;
}
Now we write logic to check the email in the shared preference. First, we check if that email exists or not and show a message Email not exist
. Then we will get the password from the sharedPreferences that is stored in mapping with the email. The data is saved as
Column 1 | Column 2 |
---|---|
myemail | mypassword |
I can get my password by calling the email that the user enters in the Email field. Then I compare the database value with the user-entered password field value.
if (!sharedPreferences.contains(email_et.getText().toString().toLowerCase())) {
Toast.makeText(SignIn_Activity.this, "Email not exist", Toast.LENGTH_SHORT).show();
} else {
String get_pass = sharedPreferences.getString(email_et.getText().toString().toLowerCase(), "");
if (pass_et.getText().toString().equals(get_pass)) {
Toast.makeText(SignIn_Activity.this, "Login Successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SignIn_Activity.this, "Login failed: Wrong password or email", Toast.LENGTH_SHORT).show();
}
}
Now let's run our app and check if all the logic is working or not.

Thank You
