4

I am able to show EditTextPreference with default summary. Now i want to update it when i enter new value in Edit Text box and click ok then it should be upadate the value of summary. But i am unable to do that. My code is below please update in my code which required.

public class Prefs extends PreferenceActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

    final EditTextPreference pref = (EditTextPreference)findPreference("username");
    pref.setSummary(pref.getText());
    pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
      public boolean onPreferenceChange(Preference preference, Object newValue) {
        final EditTextPreference pref = (EditTextPreference)findPreference("username");

        pref.setSummary(pref.getText());

        return true;
      }
    });
  }
}

xml file is

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <EditTextPreference 
    android:title="User Name"
    android:key="username" 
    android:summary="Please provide user name" />
  <EditTextPreference 
    android:title="Password" 
    android:password="true"
    android:key="password" 
    android:summary="Please enter your password" />
</PreferenceScreen>

actually my code update previous enter value as summary text. How to show text as summary when I click ok button. Thanks

Shrayas
  • 6,784
  • 11
  • 37
  • 54
atul yadav
  • 95
  • 1
  • 2
  • 5
  • SO is not a free coding service. Please take the [introductory tour](http://www.stackoverflow.com/tour). You might want to read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask), which enhances the probability for getting a useful answer _drastically_. You might find [ESR](https://en.m.wikipedia.org/wiki/Eric_S._Raymond)'s excellent essay [How To Ask Questions The Smart Way](http://catb.org/~esr/faqs/smart-questions.html) helpful. – Markus W Mahlberg Nov 14 '15 at 10:04

3 Answers3

14

You can implement this simply by subclassing EditTextPreference and overriding getSummary().

package com.example.yourapplication;

import android.content.Context;
import android.preference.EditTextPreference;
import android.text.TextUtils;
import android.util.AttributeSet;

public class FriendlyEditTextPreference extends EditTextPreference {

    public FriendlyEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

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

    // According to ListPreference implementation
    @Override
    public CharSequence getSummary() {
        String text = getText();
        if (TextUtils.isEmpty(text)) {
            CharSequence hint = getEditText().getHint();
            if (!TextUtils.isEmpty(hint)) {
                return hint;
            } else {
                return super.getSummary();
            }
        } else {
            CharSequence summary = super.getSummary();
            if (!TextUtils.isEmpty(summary)) {
                return String.format(summary.toString(), text);
            } else {
                return summary;
            }
        }
    }
}

In your xml, replace EditTextPreference with com.example.yourapplication.FriendlyEditTextPreference

Now you can use string containing "%s" or "%1$s" as android:summary and it will update automatically; moreover, you can set andorid:hint so that the hint is also displayed when text is empty.

For A working ListPreference on all platforms, see my answer of this uestion

Community
  • 1
  • 1
Hai Zhang
  • 5,574
  • 1
  • 44
  • 51
3

Your preference activity doesn't appear to be implementing OnSharedPreferenceChangeListener

You may want to read over the excellent answer to the question: How do I display the current value of an Android Preference in the Preference summary?

Community
  • 1
  • 1
dustmachine
  • 10,622
  • 5
  • 26
  • 28
0

You should use the newVal (preference.getText() will return the previous value as it's not yet updated)

It should look somthing like this:

pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        preference.setSummary((String)newValue); // TODO validate value
        return true;
    }
});
SagiLow
  • 5,721
  • 9
  • 60
  • 115