Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to change texts in the preferences activity dynamically?

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

like image 471
user1332117 Avatar asked Apr 13 '12 17:04

user1332117


2 Answers

Try like this..

 Preference customPref = (Preference) findPreference("updates_interval");<-- your preferences key
 customPref.setSummary("desired string");
like image 83
5hssba Avatar answered Oct 10 '22 14:10

5hssba


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.

like image 39
Shankar Agarwal Avatar answered Oct 10 '22 14:10

Shankar Agarwal