zeeshan
zeeshan

Reputation: 5053

How to get updated text value from EditTextPreference - it gets old value

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.

Upvotes: 1

Views: 4867

Answers (3)

vinayv
vinayv

Reputation: 1

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

String password = newValue.toString();

Upvotes: 0

Amit Kumar Khare
Amit Kumar Khare

Reputation: 573

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;
    }
});

Upvotes: 2

Dan Levin
Dan Levin

Reputation: 714

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

Upvotes: 0

Related Questions