Search code examples
javaandroidandroid-edittextsharedpreferencesandroid-sharedpreferences

SharedPreferences not working/saving data from EditText field


I am using shared preferences but my code is not working. I don't know what's wrong with it. Am I implementing something wrong?

My main java activity(relevant bit of code) is:

public class MainActivity extends AppCompatActivity {
    private String s;
    public static final String subjectKey = "SubjectID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final SharedPreferences sharedPreferences = getSharedPreferences(subjectKey, Context.MODE_PRIVATE);

    TextView calc_monday = (TextView) findViewById(R.id.monday_calc);


    calc_monday.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){



                    CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
                    cdd.show();
                    TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);

                    String text = sharedPreferences.getString(subjectKey, " ");


                    if(text != " ")
                    {
                       text1.setText(text); /* Edit the value here*/
                    }

                    TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
                    text2.setText("6 (SEECS)");
                    TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
                    text3.setText("09:00am  - 09:50am");


                }
            }
    );

    calc_monday.setOnLongClickListener(
            new Button.OnLongClickListener() {
                public boolean onLongClick(View v) {

                    SettingDialogClass SDC = new SettingDialogClass(MainActivity.this);
                    SDC.show();

                    EditText texii = (EditText) SDC.findViewById(R.id.set_Subject_ID);






                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(subjectKey, texii.getText().toString());
                    editor.apply();


                    return true;


                }
            }
    );

Basically I want that when I longClick a textbox (calc_monday), a dialog box should appear which appears. Now I should be able to write something on the EditText field which appears on this dialog box. Whatever I write should be stored and then displayed when I SINGLE CLICK on the same textbox (calc_monday)

Please see the 2 methods: onClick and onLongClick to understand.

The code is not working, i.e the text I write on EditText field on onLongCLick dialog box is not being displayed when I single click on the textbox.

What's wrong with the code


Solution

  • When you long press calc_monday, it just show your custom dialog with empty value for EditText. To save your text when input in EditText, create a button in your custom dialog, and call onClickListener action for this button, then save value to SharePreferences.

    calc_monday.setOnLongClickListener(
            new Button.OnLongClickListener() {
                public boolean onLongClick(View v) {
    
                    SettingDialogClass SDC = new SettingDialogClass(MainActivity.this);
    
                    EditText texii = (EditText) SDC.findViewById(R.id.set_Subject_ID);
    
                    Button btnSave = (Button)SDC.findViewById(R.id.your_custom_button);
                    btnSave.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString(subjectKey, texii.getText().toString());
                            editor.apply();
                            SDC.dismiss();
                        }
                    });
    
                    SDC.show();
    
                    return true;
                }
            }
    );