Save username and password using Preferences

3k views Asked by At

I'm having a trouble in using Preferences to save data login in my app. I already saved username and password at the first time but the next time i dont know how to save make autofill/autocomplete password after filled field username. Can anybody help me. Thanx so much.

1

There are 1 answers

3
Kishan Soni On

Try this code this will helps you :)

// Get SharedPreferences

 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

// set UI

  EditText username = (EditText) findViewById(R.id.username);
  EditText password = (EditText) findViewById(R.id.username);

// get value from existing preference


 strusername = sharedPreferences.getString("username", "");
 strpassword = sharedPreferences.getString("password", "");

// set existing value
username.setText(strusername);
password.setText(strpassword);

findViewById(R.id.login).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
            // put value in preference on button click
            Editor editor = sharedPreferences.edit();
            editor.putString("username", username.getText().toString());
            editor.putString("password", password.getText().toString());
            editor.commit();
        }
    });