Search code examples
androidsharedpreferencesandroid-sharedpreferences

How to update shared preference string (password) in android


**When application open immateriality will ask password.Initially password is static like(1111). When enter user in Home activity,change the password option is available.Could you please help how to do this...Thanks

1. MainActivity.java
String storedPassword = "1111"; 
strPassword=tv_password.getText().toString();
            if(storedPassword.equals(strPassword)) {
                Intent MainIntent = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(MainIntent);
            }else {
                Toast.makeText(MainActivity.this,"Please check your password",Toast.LENGTH_LONG).show();
            }   

2. Homeactivity.java
public class HomeActivity extends Activity implements View.OnClickListener {
private Button bt_change_password;
 AlertDialog.Builder alert = new AlertDialog.Builder(this);

            alert.setTitle("Edit Password");

            LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(20, 0, 30, 0);
            EditText first_password = new EditText(HomeActivity.this);
            EditText secound_password = new EditText(HomeActivity.this);
            first_password.setHint("new password");
            secound_password.setHint("new password(again)");
            layout.addView(first_password, params);
            layout.addView(secound_password, params);
            alert.setView(layout);
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // do nothing
                }
            });
            alert.show();
            break;

Solution

  • Problem is resolved...
    set Shared Pref:             
                 alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        userId= first_password.getText().toString();
                        SharedPreferences settings = PreferenceManager
                                .getDefaultSharedPreferences(HomeActivity.this);
                        SharedPreferences.Editor edit = settings.edit();
                        edit.putString("name", userId);
                        edit.commit();
                        }
    
    get shared pref:
    
    SharedPreferences settins = PreferenceManager
                .getDefaultSharedPreferences(MainActivity.this);
        strPassword=settins.getString("name", storedPassword);