Android App Development | Lecture#24 | Hive Learners

avatar

๐“–๐“ป๐“ฎ๐“ฎ๐“ฝ๐“ฒ๐“ท๐“ฐ๐“ผ

Hello dear community members, Today is our 24th lecture on Android App Development. Due to the EID Season, there is a delay for this lecture. I am sorry for that and also wish you all the EID MUBARAK. I hope you all are enjoying a good time with your friends and family members. Today we will set some checks in our code and save the data of a user in the internal storage for the signing and signup. So let's jump into it.

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

  • How to set checks
  • Save data in SharedPreferences

Assignment

  • Create Check and save data

Procedure

First, we set some checks in the signup java code. Here are the checks for the name, email, password, and a check to compare the text in the password field with the text in the confirm password field. If any check failed then it will show an error in the target field. And the return stop the code to execute further. I use a built-in function TextUtils to implement these checks.

 if (TextUtils.isEmpty(name_et.getText())) {
                    name_et.setError("Invalid Name");
                    return;
                }
if (TextUtils.isEmpty(email_et.getText())) {
                    email_et.setError("Invalid Email");
                    return;
                }
if (TextUtils.isEmpty(pass_et.getText())) {
                    pass_et.setError("Invalid Password");
                    return;
                }
if (!(pass_et.getText().toString().equals(confirm_pass_et.getText().toString()))) {
                    Toast.makeText(MainActivity.this, "Password not matching", Toast.LENGTH_SHORT).show();
                    return;
                }

Now we create a function create_user with three parameters name, email, and password. Also, declare SharedPreferece. It will be used to save data in internal storage. It is not safe for security reasons. But will only learn it to use for different purposes rather than signing signup.

    private SharedPreferences sharedPreferences;

We also need to initialize the sharedPreferences.

sharedPreferences = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);

Now we will implement the logic in the create_user function to store the data.

    public void create_user(String name, String email, String password) {
        sharedPreferences.edit().putString(email, password).apply();
        sharedPreferences.edit().putString(email + "_name", name).apply();
        Toast.makeText(this, "Account created successfully", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(MainActivity.this, SignIn_Activity.class));
        finish();
    }

Now we need to call this function in the sign_up button click after the checks. This will create a new user and the data will save in internal private storage.


hl_divider.png

Thank You

hl_footer_banner.png



0
0
0.000
8 comments
avatar

Doing the checks is very important for the software ! thanks
!1UP

You can earn passive income by delegation of tribe tokens to "The Cartel".

dlmmqb-TheCartel-banner
Click this banner to join "The Cartel" discord server to know more.

0
0
0.000
avatar

Yeah. It will prevent the wrong input.

0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.ย 
ย 

0
0
0.000