Android List Preferences: have summary as selected value?

The simplest way to do this is just to have Android do it for you. Assuming you want the summary to match the selected value, you can simply set the summary of the ListPreference to "%s" using either XML or the setSummary method in Java. For example:

<ListPreference
    android:key="pref_list"
    android:title="A list of preferences"
    android:summary="%s"
    android:entries="@array/pref_list_entries"
    android:entryValues="@array/pref_list_entries_values"
    android:defaultValue="0" />

Android will replace %s with the current string value of the preference, as displayed by the ListPreference‘s picker. The list’s summary will also be set correctly when you enter the activity—you don’t have to write any special code to set it up initially.

This also works with the AndroidX ListPreference.

I spent far too much time mucking with SharedPreferences listeners before I discovered this.

Leave a Comment