This must come up very often.
When the user is editing preferences in an Android app, I'd like them to be able to see the currently set value of the preference in the Preference
summary.
Example: if I have a Preference setting for "Discard old messages" that specifies the number of days after which messages need to be cleaned up. In the PreferenceActivity
I'd like the user to see:
"Discard old messages" <- title
"Clean up messages after x days" <- summary where x is the current Preference value
Extra credit: make this reusable, so I can easily apply it to all my preferences regardless of their type (so that it work with EditTextPreference, ListPreference etc. with minimal amount of coding).
I solved the issue with the following descendant of ListPreference:
Seems to work fine for me in 1.6 up through 4.0.4.
Because I'm using a custom
PreferenceDataStore
, I can't add a listener to someSharedPreference
so I've had to write a somewhat hacky solution that listens to each preference:You can override default Preference classes and implement the feature.
Later in you xml you can use custom preference like
Here,all these are cut from Eclipse sample
SettingsActivity
. I have to copy all these too much codes to show how these android developers choose perfectly for more generalized and stable coding style.I left the codes for adapting the
PreferenceActivity
to tablet and greater API.xml/pref_general.xml
values/strings_activity_settings.xml
NOTE: Actually I just want to comment like "Google's sample for PreferenceActivity is also interesting". But I haven't enough reputation points.So please don't blame me.
(Sorry for bad English)
If someone is still looking for answers to this, you should check out thirtythreefortys answer.