0

I've added an EditTextPreference to my Settings.xml with the code below

    <EditTextPreference
        android:key="prefDeviceUser"
        android:title="User Name"
        android:summary="Please add user name." />

When I run it the dialog box appears but when I add my name it doesn't update and still says 'Please add..' What have I left out?

user616076
  • 3,907
  • 8
  • 38
  • 64
  • By default EditTextPreference displays always same values. To have Your target You need to create Your own EditTextPreference, where You can override summary. In this case You can use String.format for example. – madoxdev Feb 08 '16 at 06:41
  • You can update `EditText` in your `onResume()` method. There is no need for creating custom `EditText`. – Stanojkovic Feb 08 '16 at 07:49
  • Updating EditText in my onResume(), could you please expand on that a little, like where would the dialog value be? – user616076 Feb 08 '16 at 08:29

1 Answers1

1

Please create Your own EditTextPreference. To achieve Your result You need to override getSummary method.

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());
    }
}

Then I use in preferences.xml file, newly created EditTextPreference, and in summary I set value from strings.xml like this:

android:summary="@string/string_val"

And finally in strings.xml it looks like this:

Please select user. Currently set is: %s

Final summary is then: Please select user. Currently set is: selectedValue

madoxdev
  • 3,770
  • 1
  • 24
  • 39