Displaying EditTextPreference current value in summary

Viewed 2664

I want to be able to display the value of an EditTextPreference in the summary field. Specifically, I want to do this within PreferenceFragmentCompat.

import android.support.v7.preference.PreferenceFragmentCompat;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences);
    }
}

Preference file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference
        android:defaultValue="DEVICE01"
        android:key="device_id"
        android:title="Device ID" />
</PreferenceScreen>

I have seen other solutions, but none of them included how to do this within PreferenceFragmentCompat.

3 Answers

As this question still pops up on Google, there is now a more direct way to solve this (using androidx).

<PreferenceScreen 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <EditTextPreference
        android:key="prefFoobar"
        android:title="@string/preference_foobar"
        android:persistent="true"
        app:useSimpleSummaryProvider="true" />
</PreferenceScreen>

The "app:useSimpleSummaryProvider" does all the magic.

Related