5

When I'm trying to set summary when the users select a preference item, it normally saved. But when my app is restarted, the summary is gone.

Here is my code to set the summary for ListPreference and EditTextPreference:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){

    Preference pref = findPreference(key);

    // I feel the problem is happened here
    if (pref instanceof ListPreference) {
        ListPreference listPref = (ListPreference) pref;
        pref.setSummary(listPref.getEntry());
    }
    // Same problem here
    if (pref instanceof EditTextPreference) {
        EditTextPreference editText = (EditTextPreference) pref;
        pref.setSummary(editText.getEntry().toString());
    }
}

Is there something wrong?

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
  • 3
    you should set the summary even in oncreate – nandeesh Sep 30 '14 at 07:28
  • 1
    What about the parameter? This parameter [SharedPreferences sharedPreferences, String key] is associated with Preference pref = findPreference(key). In onCreate, this parameter (String key) is not available. – Anggrayudi H Sep 30 '14 at 07:47
  • 1
    if it is not, then add android:key in the xml – nandeesh Sep 30 '14 at 07:59
  • 1
    But I have added android:key for every preferences in the xml. – Anggrayudi H Sep 30 '14 at 08:21
  • Does this answer your question? [How to set edittext preference summary and have it stick](https://stackoverflow.com/questions/22651653/how-to-set-edittext-preference-summary-and-have-it-stick) – Mahozad Sep 23 '20 at 10:03

4 Answers4

27

if you only want to show the current entry, try to set the summary in your xml:

android:summary="%s"

This works only for the ListPreference (see Doc):

If the summary has a String formatting marker in it (i.e. "%s" or "%1$s"), then the current entry value will be substituted in its place.

FreshD
  • 2,914
  • 2
  • 23
  • 34
7

The problem might be that the listener is not called on startup (the value is not changed). But you can set the summary dynamically in the XML. For a ListPreference, this is built-in and @FreshD's answer is the way to go. To extend to an EditTextPreference, you need to create your own class. For example

package your.package;

import android.content.Context;
import android.util.AttributeSet;

public class EditTextPreference extends android.preference.EditTextPreference{
        public EditTextPreference(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public EditTextPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public EditTextPreference(Context context) {
            super(context);
        }

        @Override
        public CharSequence getSummary() {
            String summary = super.getSummary().toString();
            return String.format(summary, getText());
        }
    }

And use this in your xml:

<your.package.EditTextPreference
                android:key="pref_alpha"
                android:summary="Actual value: %s"
                android:title="Title"
                android:defaultValue="default"
                />
Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
serv-inc
  • 35,772
  • 9
  • 166
  • 188
3
 ListPreference listPref = (ListPreference) findPreference("listkey");
 listPref.setSummary(listPref.getEntry());

 EditTextPreference editText = (EditTextPreference) findPreference("edittextkey");
 editText.setSummary(editText.getEntry().toString());

If you have the key then set the summary like above, in oncreate after addpreferences in your preferenceFragment or Activity

nandeesh
  • 24,740
  • 6
  • 69
  • 79
2

I struggled so much to make summaries dynamic. Not exactly sure if this answers the question but wanted to post somewhere so if someone searches they don't have to struggle like i did.

This is the simple solution useSimpleSummaryProvider="true"

Maelig
  • 2,046
  • 4
  • 24
  • 49