Reputation: 33
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.
Upvotes: 2
Views: 2948
Reputation: 3264
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.
Upvotes: 9
Reputation: 33
The answer from @grabarz121 set me in the right direction. This is what I added to the preferences:
android:summary="@string/pref_device_id_summary"
This is what I modified/added to the SettingsFragment class:
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
setDeviceIdSummary();
}
public void setDeviceIdSummary() {
final String deviceIdKey = getString(R.string.pref_device_id_key);
String deviceIdDefault = getString(R.string.pref_device_id_default);
String deviceIdValue = sharedPreferences.getString(deviceIdKey, deviceIdDefault);
final EditTextPreference editTextPreference = (EditTextPreference) findPreference(deviceIdKey);
editTextPreference.setSummary(deviceIdValue);
editTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
String newValue = o.toString();
sharedPreferences.edit().putString(deviceIdKey, newValue).apply();
editTextPreference.setSummary(newValue);
return true;
}
});
}
Upvotes: 1
Reputation: 656
I think, it's the same way as in every simple PreferenceFragment: In xml file:
android:summary="@string/your_string_resource"
In code:
EditTextPreference editTextPreference = (EditTextPreference) findPreference(YOUR_PREFERENCE_KEY);
editTextPreference.setSummary(sharedPreferences.getString(YOUR_PREFERENCE_KEY, defaultValue));
editTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
String yourString = o.toString();
sharedPreferences.edit().putString(YOUR_PREFERENCE_KEY, yourString).apply();
editTextPreference.setSummary(yourString);
return true;
}
});
Upvotes: 1