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).
Android documentation says one can use a String formatting marker in
getSummary()
:Simply specifying
android:summary="Clean up messages after %s days"
in ListPreference xml declaration worked for me.Note: This only works for
ListPreference
.Here is a working solution for all
EditTextPreference
s inside of aPreferenceFragment
based on @tdeveaux answer:I found this way to make
EditTextPreference
from support library handle"%s"
in summary (asListPreference
already handles):In xml it will look like this:
My solution is to create a custom
EditTextPreference
, used in XML like this:<com.example.EditTextPreference android:title="Example Title" />
EditTextPreference.java:-
After several hours I've been spent to solve such problem I've implemented this code:
[UPDATE: the final version listing]
That was the only solution that worked for me fine. Before I've tried to subclass from ListPreferences and to implement android:summary="bla bla bla %s". Neither worked.
This is the code you need to set the summary to the chosen value. It also sets the values on startup and respects the default values, not only on change. Just change "R.layout.prefs" to your xml-file and extend the setSummary-method to your needs. It actually is only handling ListPreferences, but it is easy to customize to respect other Preferences.
koem