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).
For EditTextPreference:
I came to this solution, of course, just if you need particular edittextpreference but you could do this with every Preference:
............
............
Then in onResume();
In:
Thanks, Reto, for the detailed explanation!
In case this is of any help to anyone, I had to change the code proposed by Reto Meier to make it work with the SDK for Android 1.5
The same change applies for the callback function
onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
Cheers,
Chris
Here's my solution:
Build a preference type 'getter' method.
Build a 'setSummaryInit' method.
Initialize
Create public class that PreferenceActivity and implements OnSharedPreferenceChangeListener
If you use
PreferenceFragment
, this is how I solved it. It's self explanatory.For EditTextPreference:
Simply: