Login Screen: Remember Me Example


We observed to save a credentitial of site in browser cache. Now we will learn; how to cache the credntitials in Android Application?

We can cache the credentitial using sharedprefrence.



File Name
Code
XML Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="20dp"
  tools:context=".MainActivity">

  <EditText
      android:id="@+id/email"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Enter your email address here"
      android:inputType="textEmailAddress"
      android:padding="10dp" />

  <EditText
      android:id="@+id/username"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Enter User Name Here"
      android:inputType="text"
      android:padding="10dp" />

  <EditText
      android:id="@+id/password"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Enter your password here"
      android:inputType="textPassword"
      android:padding="10dp" />

  <CheckBox
      android:id="@+id/remember"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Remember Me"
      android:textColor="@color/colorPrimaryDark" />

  <Button
      android:id="@+id/login"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorPrimaryDark"
      android:padding="10dp"
      android:text="Login" />

</LinearLayout>
Java Code
MainActivity.java
public class MainActivity extends AppCompatActivity {
  EditText email,password,userName;
  Button login;
  CheckBox rememberMe;
  /////////////////////
  SharedPreferences sharedPreferences;
  SharedPreferences.Editor editor;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      email=findViewById(R.id.email);
      password=findViewById(R.id.password);
      userName=findViewById(R.id.username);
      login=findViewById(R.id.login);
      rememberMe=findViewById(R.id.remember);
      ////////////////////////////////////
      sharedPreferences=getSharedPreferences("LoginPrefs", MODE_PRIVATE);
      editor=sharedPreferences.edit();
      /////////////////To get Stored Data/////////////////////////////////
      String mail=sharedPreferences.getString("email","No Email Stored");
      String name=sharedPreferences.getString("name","Un Named");
      String passwords=sharedPreferences.getString("passowrd","");
      ////////////////////////////////////////////////////////////////////
      email.setText(mail);
      password.setText(passwords);
      userName.setText(name);

      login.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              ///////////////To Store Data////////////////////////////
              if(rememberMe.isChecked()){
                  editor.putString("email",email.getText().toString());
                  editor.putString("name",userName.getText().toString());
                  editor.putString("passowrd",password.getText().toString());
                  editor.commit();
              }else{
                  editor.putString("email","");
                  editor.putString("name","");
                  editor.putString("passowrd","");
                  editor.commit();
              }
              //////////////////////////////////////////////////////////////
           Intent intent=new Intent(MainActivity.this,SecondActivity .class);
              startActivity(intent);
          }
      });
  }
}
XML Layout
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
      android:id="@+id/msg"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>
Java Code
SecondActivity.java
public class SecondActivity extends Activity {
  TextView msg;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.second_layout);
   }
}

Comments