What I usually do is:
1 - Make a new class which extends the kind of preference I need to show (1 per preference type) 2 - Inside its code, do the appropriate actiob to show the updated summary
3 - Refer this class in the res/xml/preferences.xml file
Let me swo a small example, good for an EditTextPreference:
CLS_Prefs_Edit.java
/** * CLS_Prefs_Edit class * * This is the class that allows for a custom EditTextPrefence * (auto refresh summary). * * @category Custom Preference * @author Luca Crisi ([email protected]) * @copyright Luca Crisi * @version 1.0 */ package com.your_name.your_app; /* -------------------------------- Imports --------------------------------- */ import android.content.Context; import android.preference.EditTextPreference; import android.util.AttributeSet; public final class CLS_Prefs_Edit extends EditTextPreference { /* ---------------------------- Constructors ---------------------------- */ public CLS_Prefs_Edit(final Context ctx, final AttributeSet attrs) { super(ctx, attrs); } public CLS_Prefs_Edit(final Context ctx) { super(ctx); } /* ----------------------------- Overrides ------------------------------ */ @Override public void setText(final String value) { super.setText(value); setSummary(getText()); } }
res/xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="@string/pref_phone_cat"> <!-- NORMAL EditTextPreference, NO summary update --> <!-- <EditTextPreference --> <!-- android:widgetLayout="@layout/arr_dn" --> <!-- android:key="phone" --> <!-- android:title="@string/pref_phone_title" --> <!-- android:summary="@string/pref_phone_summ" --> <!-- android:defaultValue="" --> <!-- android:inputType="phone" --> <!-- android:digits="+1234567890" --> <!-- /> --> <!-- MY EditTextPreference, WITH summary update --> <com.your_name.your_app.CLS_Prefs_Edit android:widgetLayout="@layout/arr_dn" android:key="phone" android:title="@string/pref_phone_title" android:summary="@string/pref_phone_summ" android:defaultValue="" android:inputType="phone" android:digits="+1234567890" /> </PreferenceCategory> </PreferenceScreen>
Of course, set your strings in /res/values/strings, and you're done.
Note that this solution works for both PreferenceFragments and PreferenceActivities.
I'm using it for an app tha runs on 2.2 Froyo (showing a PreferenceActivity) as well as on 4.4 KitKat (showing a PreferenceFragment)
I hope it helps.