3

I feel like I must be missing something, but I just don't see what it is... I have a PreferenceActivity with a bunch of various preferences (some are lists, some are just text fields) and it all works fine, but unless I explicitly write each item's value to the summary (which is obviously not intended for this purpose) I don't see how (or where) the items display what they are currently set to. When I click on them the various views show up with the correct settings, but that's clearly not the intention.

Do I have to create my own custom List item of some sort that has a field that displays the currently populated value of each element?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • Possible duplicate of [How do I display the current value of an Android Preference in the Preference summary?](https://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-su) – serv-inc May 23 '17 at 14:15

4 Answers4

2

Unfortunately the default PreferencesActivity doesn't display the values: what you're doing is really the way to go if you care to have all the preferences displayed at a glance.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I had a feeling that that was the case, but it's just bizarre that they would include such a useless mechanism... What's the point? It's not connected to my default shared preferences, right? So, it's not even a short cut to setting those (initially I thought maybe it was and the key field was used to look up my shared prefs, but that doesn't appear to be the case). So... it's just a List view that holds on to its own values? I MUST be missing something! – Yevgeny Simkin May 07 '11 at 16:54
  • 2
    Well, it is actually connected to your default shared preferences, so unless you're making a mistake elsewhere that SHOULD work. If you add an `addPreferencesFromResource(R.xml.prefs);` call in the `onCreate()` method it will pull those up. This assumes your XML preferences file is called *prefs.xml* in the *res/xml* folder. – Femi May 07 '11 at 17:07
  • interesting... I had a different name for my xml file... I'll see if that addresses the disparity. – Yevgeny Simkin May 16 '11 at 19:42
2

If you still want to go down the programming direction then look at this thread: How do I display the current value of an Android Preference in the Preference summary?

Has everything there.

Community
  • 1
  • 1
theblitz
  • 6,683
  • 16
  • 60
  • 114
1

Create another preference field: summary. Update it whenever a preference field is updated, or when displaying the preferences screen. The user will be able to "update" the summary value, but whenever he/she enters preferences, the correct value will be displayed.

Yar
  • 4,543
  • 2
  • 35
  • 42
0

For ListPreferences, this is built-in and you can use

android:summary="Actual value: %s"

For EditTextPreferences, you can easily create your own class:

package your.package.preference;

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"
                />
serv-inc
  • 35,772
  • 9
  • 166
  • 188