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).
The concise solution by 1 line of code:
FYI:
All the more reason to look at the very slick
Answer
of @ASD above (source found here) saying to use%s
inandroid:summary
for each field inpreferences.xml
. (Current value of preference is substituted for%s
.)To set the summary of a
ListPreference
to the value selected in a dialog you could use this code:and reference the
yourpackage.ListPreference
object in yourpreferences.xml
remembering to specify there yourandroid:defaultValue
as this triggers the call toonSetInitialValue()
.If you want you can then modify the text before calling
setSummary()
to whatever suits your application.Actually, CheckBoxPreference does have the ability to specify a different summary based on the checkbox value. See the android:summaryOff and android:summaryOn attributes (as well as the corresponding CheckBoxPreference methods).
Thanks for this tip!
I have one preference screen and want to show the value for each list preference as the summary.
This is my way now:
This works for me, but I'm wondering what is the best solution (performance, stability, scalibility): the one Koem is showing or this one?
Maybe like ListPreference: Modify getSummary to get what you want:
And use this in your xml:
So you are able to write a summary with
%s
instead of the actual value.