1

I am trying to setup a new password using EditTextPreference. In this Android 4.0.3, clicking the EditTextPreference opens a dialog window where user can enter text. However, on pressing ok, it still shows the old text value and not the newly entered value.

public class MyPreferencesActivity extends PreferenceActivity {

EditTextPreference edp_password = (EditTextPreference) findPreference("pref_key_account_password");

edp_password.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
        public boolean onPreferenceChange(Preference preference, Object newValue) {
             String password = edp_password.getText();
            Log.v(TAG, "Password is: " + password);
            return true;
        }
    });

I spend a while trying to make it work, but couldn't find any good solution. How can I retrive the newly entered text after user presses Ok.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
zeeshan
  • 4,913
  • 1
  • 49
  • 58
  • just some advice for storing passwords: http://stackoverflow.com/questions/785973/what-is-the-most-appropriate-way-to-store-user-settings-in-android-application – wrapperapps May 26 '15 at 14:41
  • Thanks. In real apps I prepend them with a re-generatable value which only I could correctly generate, then encrypt the whole string before saving. – zeeshan May 26 '15 at 14:58

3 Answers3

2

Try this way:

EditTextPreference pref_dayCount = (EditTextPreference)findPreference("pref_dayCount");
pref_dayCount.setDefaultValue(30);
pref_dayCount.setSummary(getResources().getString(R.string.pref_plan_days_number_summary)+" "+pref_dayCount.getText());
pref_dayCount.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
            preference.setSummary(getResources().getString(R.string.pref_plan_days_number_summary)+" "+newValue.toString());    
        return true;
    }
});
Amit Kumar Khare
  • 565
  • 6
  • 17
0

ok i think i know what the problem is, from the android developers references, it says that this callback is invoked before the internal state has been updated, so i guess that why you see the old value.

the way you did it was fine, but i'm guessing you need to use the newValue object, maybe something like this:

public class MyPreferencesActivity extends PreferenceActivity {

EditTextPreference edp_password = (EditTextPreference)       findPreference("pref_key_account_password");

edp_password.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
    public boolean onPreferenceChange(Preference preference, Object newValue) {

        Log.v(TAG, "Password is: " + (EditText)newValue.getText());
        return true;
    }
});

i'm not sure if this works, i only free write it :P

Dan Levin
  • 718
  • 7
  • 16
  • This helped indeed for my current situation. Thanks for posting the code too. TextWatcher was my backup option, I have used it once before, but since it watches for every key stroke, which is kind of unnecessary, so I was wanting something more elegant way to accomplish this, i.e. after pressing Ok you just get the final input. However if there is no other way to do it, I'll accept your answer as the final answer. Maybe this will encourage me to expand my code to make it AJAX type password check. – zeeshan Jun 17 '13 at 02:57
  • ok i think i know what the problem is, from the android developers references, it says that this callback is invoked before the internal state has been updated, so i guess that why you see the old value. – Dan Levin Jun 17 '13 at 08:36
  • You are right, I also read that. But I was wondering that there should be a way to get what is updated after the text has changed. I have to add a few more EditTextPreferences too, and would have been cleaner to retrieve new values using some simpler code using something like getText(). – zeeshan Jun 17 '13 at 20:08
0

password.getText will not get the updated value. Use:

String password = newValue.toString();
venerik
  • 5,766
  • 2
  • 33
  • 43
vinayv
  • 1
  • 3