Vishwa Madushan Vishwa Madushan - 3 months ago 12
Java Question

Update Textview right after user change settings value

In my app I let the user choose a name to display. It should display as:
Hello

usertext
!

I can successfully update settings. I used PreferenceActivity.
I can change the value too. But to change the text in the selected textview, I have to restart the app.

How can I update my textview right after users update the value in settings?

Answer
Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // Code goes here            
        return true;
    }
};

EditTextPreference pref = (EditTextPreference)findPreference(getString(R.string.pref1));
pref1.setOnPreferenceChangeListener(changeListener);

EditTextPreference pref2 = (EditTextPreference)findPreference(getString(R.string.pref2));
pref2.setOnPreferenceChangeListener(changeListener);
Comments