Unfortunately there doesn't seem to be a simple, automated way of doing this (you'll notice that very few of the 'native' preferences do this). That said, I'm sure it's a pretty common requirement, so here's the technique I use to achieve it.
The key is to make your PreferenceActivity
class implement OnSharedPreferenceChangeListener
. Using the onSharedPreferenceChanged
method you can listen for specific preference keys and use setSummary
on the related preference control to modify the text, like this:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Let's do something a preference value changes
if (key.equals(KEY_A_CHECKBOX_PREFERENCE)) {
mCheckBoxPreference.setSummary(sharedPreferences.getBoolean(key, false) ? "Disable this setting" : "Enable this setting");
}
else if (key.equals(KEY_AN_EDITTEXT_PREFERENCE)) {
mEditBoxPreference.setSummary("Current value is " + sharedPreferences.getString(key, ""));
}
}
Listen for changes in preference using the registerOnSharedPreferenceChangeListener
method on the applications Shared Preferences to assign this class as a listener, like this:
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
Your best bet is to register it in onResume
and deregister on onPause
. You'll also need to update the text values when you register the listener to ensure you get the right initial values.
The following example is based on the AdvancedPreferences example project in the Android code samples.
public class AdvancedPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
public static final String KEY_LIST_PREFERENCE = "list_preference";
public static final String KEY_CHECKBOX_PREFERENCE = "checkbox_preference";
private CheckBoxPreference mCheckBoxPreference;
private ListPreference mListPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the XML preferences file
addPreferencesFromResource(R.xml.advanced_preferences);
// Get a reference to the preferences
mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(KEY_ADVANCED_CHECKBOX_PREFERENCE);
mListPreference = (ListPreference)getPreferenceScreen().findPreference(KEY_LIST_PREFERENCE);
}
@Override
protected void onResume() {
super.onResume();
// Setup the initial values
mCheckBoxPreference.setSummary(sharedPreferences.getBoolean(key, false) ? "Disable this setting" : "Enable this setting");
mListPreference.setSummary("Current value is " + sharedPreferences.getValue(key, ""));
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Let's do something a preference value changes
if (key.equals(KEY_CHECKBOX_PREFERENCE)) {
mCheckBoxPreference.setSummary(sharedPreferences.getBoolean(key, false) ? "Disable this setting" : "Enable this setting");
}
else if (key.equals(KEY_LIST_PREFERENCE)) {
mListPreference.setSummary("Current value is " + sharedPreferences.getValue(key, ""));
}
}
}