Search code examples
androidpreferences

getting the saved password in android


I am creating an android device in which there ia a login activity. how to check whether a certain id and password is saved in android device earlier and if it is saved then when the user has entered the emailid the password should get automatically set in the password field. I have saved the email id and password as

if ((savepass).isChecked()) {

                    getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
                        .edit()
                        .putString(PREF_USERNAME, eMailId)
                        .putString(PREF_PASSWORD, PWOrd)
                        .commit();
              }

can anyone please help me.. thanks


Solution

    1. Get saved username and PW:

      SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
      String pw = pref.getString(PREF_PASSWORD, null);
      String username = pref.getString(PREF_USERNAME, null);
      

      if either is null, then username/password are not saved.

    2. Set the password / username in the relevant fields:

      userNameTextView.setText(username);
      passwordTextView.setText(pw);