Masoom Bacha Masoom Bacha - 5 months ago 12
Android Question

how to save data in editText using Shared Preferences in android

i have two edittext i want to save data in edittext until i have not change it how to made it . i try some thing like this but data not save.

e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String homewifi = e1.getText().toString();
String officewifi = e2.getText().toString();

SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString(HOMEWIFI, homewifi);
editor.putString(OFFICEWIFI, officewifi);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

}
});


when activity open again data not show in edittext.

Answer

You are not retrieving data from sharedpreferences modify code like this:

 sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);  
     SharedPreferences.Editor editor = sharedpreferences.edit();

    e1 = (EditText)findViewById(R.id.editText);
    e2 = (EditText)findViewById(R.id.editText2);

    String homewifi = sharedpreferences.getString(HOMEWIFI, "");
    String officeWifi = sharedpreferences.getString(OFFICEWIFI, "");

    e1.setText(homewifi);
    e2.setText(officeWifi);


        b1 = (Button)findViewById(R.id.button3);

       Context.MODE_PRIVATE);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String homewifi  = e1.getText().toString();
                String officewifi  = e2.getText().toString();



                editor.putString(HOMEWIFI, homewifi);
                editor.putString(OFFICEWIFI, officewifi);
                editor.commit();
                Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

            }
        });
Comments