Home > OS >  ANDROID STUDIO, Something written in editText should stay there untill the user edits it again. it s
ANDROID STUDIO, Something written in editText should stay there untill the user edits it again. it s

Time:07-10

I am trying to make an app in android studio. I want my app to store what user types in the app in editText. Even if user opens the app after 1 week it should stay there in edit text. thanks.

CodePudding user response:

Steps to follow When the app goes to idle or background save the data written in edittext with preferences check the preference when app is opened and prefill the edittext. If you want coding help then let me know. Hope this will give you and give you tha bacis

CodePudding user response:

The most simple way to achieve this is by using SharedPreferences

To detect when the user edits the value of the editText, use addTextChangedListener, and immediately store it using SharedPreferences The code for this is as following,

editText.addTextChangedListener(new TextChangedListener<EditText>(editText) {
        @Override
        public void onTextChanged(EditText target, Editable s) {
            SharedPreferences.Editor editor = save.edit();
            editor.putString("KEY", editText.getText().toString()).apply();
        }
    });

The above code will save the editText value. To retrieve the stored value when the user opens the app later, the following code shall be used,

SharedPreferences editTextPref = getApplicationContext().getSharedPreferences("KEY", Context.MODE_PRIVATE);
editText.setText(editTextPref.getString("KEY"));
  • Related