How to Display the Current Value of an Android Preference in the Preference Summary

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

Displaying EditTextPreference current value in summary

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

Setting UI preference Summary field to the value of the preference

I am new too so may not be the best code but this is similar to what I am doing. You probably want to register you listener onResume and unregister it onPause though rather than onCreate. I hope this helps.

Mainly you just need to grab the pref, the pref value and set the summary.

public class MyPreferenceActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
EditTextPreference editTextPref = (EditTextPreference) findPreference("thePrefKey");
editTextPref
.setSummary(sp.getString("thePrefKey", "Some Default Text"));
}

protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}

protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Preference pref = findPreference(key);
if (pref instanceof EditTextPreference) {
EditTextPreference etp = (EditTextPreference) pref;
pref.setSummary(etp.getText());
}
}
}

Preference summary with current value throug custom EditTextPreference: how to get current value?

Try to add the following code to your EditTextPreferenceWithSummary class:

@Override
protected View onCreateView(ViewGroup parent) {
this.setSummary(this.getText());
return super.onCreateView(parent);
}

As for me, it works.
I think the problem was that you were trying to change the UI state out of UI thread (in fact in the constructor).



Related Topics



Leave a reply



Submit