Hello fellow programmers, I have a little problem with Preferences activity.
http://developer.android.com/reference/android/preference/PreferenceActivity.html
I've got just one preference category and a listPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="@string/basic_settings" >
<ListPreference
android:defaultValue="70"
android:entries="@array/listArray"
android:entryValues="@array/listValues"
android:key="updates_interval"
android:persistent="true"
android:summary="@string/SOME_SUMMARY"
android:title="@string/SOME_TITLE" />
</PreferenceCategory>
I need to have the selected value (the default one or the user defined one) written in the summary of the listPreference, for example: We will have at least 70 characters.
How can I do this from the code?
Any help is appreciated
Try like this..
Preference customPref = (Preference) findPreference("updates_interval");<-- your preferences key
customPref.setSummary("desired string");
here is a short example:
Preference etp = (Preference) findPreference("the_pref_key");
etp.setSummary("New summary");
This requires that you display your preferences either from a PreferenceActivity or from a PreferenceFragment, since findPreference() is a method of these classes. You most likely do that already.
To change the summary every time the user changes the actual preference, use a OnPreferenceChangeListener and check if the relevant key changed in the callback. After it has changed, just edit the summary like above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With