ListPreference items in Preference screens do not automatically provide a label showing their current value. You can create labels for all list Preference items as so:
In the Settings Fragment onCreatePreferences override, get the Shared Preferences and the Preference screen. Then iterate through all of the Preferences in the screen, checking whether they are tickboxes. Any that aren't will have their value stored and passed to a setPreferenceSummary function along with the Preference itself:
@Override public void onCreatePreferences(Bundle bundle, String s) { addPreferencesFromResource(R.xml.pref_xxxxx); // Reference to xml resource SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences(); // Retrieve the Shared Preferences from the Preference screen PreferenceScreen preferenceScreen = getPreferenceScreen(); // Retrieve the Preference screen int count = preferenceScreen.getPreferenceCount(); // The number of Preferences within the screen for (int i = 0; i < count; i++ ) { // For each Preference... Preference p = preferenceScreen.getPreference(i); // ...retrieve the Preference... if (!(p instanceof CheckBoxPreference)) { // ...make sure it isn't a checkbox... String value = sharedPreferences.getString(p.getKey(), ""); // ...then retrieve its value (can't use getString on a Boolean, so another reason to exclude tickboxes)... setPreferenceSummary(p, value); // ...and send the Preference and its value to the setPreferenceSummary function } } }The setPreferenceSummary function is as follows:
private void setPreferenceSummary(Preference preference, String value) { if (preference instanceof ListPreference) { // If it is a ListPreference... ListPreference listPreference = (ListPreference) preference; // ...cast it to an object of type ListPreference... int prefIndex = listPreference.findIndexOfValue(value); // ...retrieve the index of that value... if (prefIndex >= 0) { listPreference.setSummary(listPreference.getEntries()[prefIndex]); // ...and get the corresponding label for it, setting it as the Preference Summary } } else if (preference instanceof EditTextPreference) { // For EditTextPreferences, set the summary to the value's simple string representation. preference.setSummary(value); } }Implement an OnSharedPreferenceChangeListener to make sure changes are implemented without having to force an onCreate (e.g. device rotation). Within the required onSharedPreferenceChanged override, again check to see if the preference is not a checkbox, and if so send the preference to the setPreferenceSummary function along with its value:
public class SettingsFragment extends PreferenceFragmentCompat implements OnSharedPreferenceChangeListener { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Preference preference = findPreference(key); if (preference != null) { if (!(preference instanceof CheckBoxPreference)) { String value = sharedPreferences.getString(preference.getKey(), ""); setPreferenceSummary(preference, value); } } } }Register and unregister the OnSharedPreferenceChangeListener with the following overrides:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } @Override public void onDestroy() { super.onDestroy(); getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); }ud851-Exercises-student\Lesson06-Visualizer-Preferences\T06.08

