Display the value of the EditTextPreference in summary
Just use the setSummary method on the desired Preference object. Call it upon resuming your settings fragment for each entry that you wish to update (i.e., all the EditTextPreference
entries in your case) and register an OnSharedPreferenceChangeListener
on the concrete SharedPreferences
object (so that you can update the summary in case it is changed) – and pass it the desired EditTextPreference's value (which you can obtain via its getText()
method).
Implement it in your MyPreferenceFragment
like this (I don't guarantee that it will work right of the bat, it serves the purpose to just give you an idea):
public class MyPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences sharedPreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load the preferences from your XML resource (which I assume you already do anyway)
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onResume() {
super.onResume();
sharedPreferences = getPreferenceManager().getSharedPreferences();
// we want to watch the preference values' changes
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
Map<String, ?> preferencesMap = sharedPreferences.getAll();
// iterate through the preference entries and update their summary if they are an instance of EditTextPreference
for (Map.Entry<String, ?> preferenceEntry : preferencesMap.entrySet()) {
if (preferenceEntry instanceof EditTextPreference) {
updateSummary((EditTextPreference) preferenceEntry);
}
}
}
@Override
public void onPause() {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Map<String, ?> preferencesMap = sharedPreferences.getAll();
// get the preference that has been changed
Object changedPreference = preferencesMap.get(key);
// and if it's an instance of EditTextPreference class, update its summary
if (preferencesMap.get(key) instanceof EditTextPreference) {
updateSummary((EditTextPreference) changedPreference);
}
}
private void updateSummary(EditTextPreference preference) {
// set the EditTextPreference's summary value to its current text
preference.setSummary(preference.getText());
}
}
If you are using AndroidX Preference library you can use the property app:useSimpleSummaryProvider. For example:
<EditTextPreference
app:key="edittext"
app:title="@string/title_edittext_preference"
app:useSimpleSummaryProvider="true"
app:dialogTitle="@string/dialog_title_edittext_preference"/>
You can read a fully working example here.
Just override getSummary
of EditTextPreference
, then you will get a EditTextPreference with its value displayed as summary.
public class EditSummaryPreference extends EditTextPreference {
...// omit constructor
@Override
public CharSequence getSummary() {
return getText();
}
}