Update Summary for PreferenceActivity

I have a PreferenceActivity with a PreferenceScreen s level 2 tree, something like:

 <PreferenceScreen> <PreferenceScreen android:key="A"> <ListPreference/> <EditTextPreference/> </PreferenceScreen> <PreferenceScreen android:key="B"> <ListPreference/> <EditTextPreference/> </PreferenceScreen> ... </PreferenceScreen> 

Each of the lower level preference screens, such as A and B, collects two related pieces of data. I want the summary for these parent elements to be a combination of the current values โ€‹โ€‹of two additional settings.

I tried adding onPreferenceChangeListener to the sheet preferences and updating the resume from there, but it does not seem to accept. All settings are created programmatically as part of the action, so I do something similar in onCreate:

  leafListPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // do some work prefScreenA.setSummary( /* get new summary based on newValue */); return true; } }); 

Then I tried to find a place where I was informed that I returned to the top-level settings screen from the subpage so that I could update this point. However, I am confused by the way lower-level screens are displayed. It seems that they are actually dialogs, not full actions, since onPause / onResume is not called when moving between them. Is there some method somewhere that I am missing that will be called when returning to the top-level page?

I also tried adding a sharedPreferenceChangeListener as described here , but this is never called.

Does anyone have any hints of what I am missing here, or any easier approach that I am missing?

+7
source share
4 answers

I had the same problem and it seems that calling Activity.onContentChanged () after changing the resume fixed my problem.

 @Override public void onSharedPreferenceChanged(SharedPreferences sp, String key) { // Update summary value EditTextPreference pref = (EditTextPreference)findPreference(key); pref.setSummary(pref.getText()); this.onContentChanged(); } 
+11
source

Use the following code and override onContentChanged ()

  public class SettingsActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); setContentView(R.layout.settings_layout); } @Override public void onContentChanged() { // put your code here super.onContentChanged(); } } 

Hope this helps.

0
source

I also ran into this problem, and I finally found a solution using the value coming from the listener. In my example below (for ListPreference), I first get the index of the value in the ListPreference array, then I retrieve the value label using this index:

 passwordFrequencyLP.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { int newFrequency = Integer.valueOf(newValue.toString()); prefs.edit().putInt("settings_key_password_frequency", newFrequency).commit(); //get the index of the new value selected in the ListPreference array int index = passwordFrequencyLP.findIndexOfValue(String.valueOf(newValue)); //get the label of the new value selected String label = (String) passwordFrequencyLP.getEntries()[index]; passwordFrequencyLP.setSummary(label); makeToast(getResources().getString(R.string.password_frequency_saved)); return true; } }); 

This little trick works well, I found many different possible solutions to this problem, but only this one worked for me.

0
source

After a really crazy amount of trial and error, I finally found the answer in another SO answer . Here's the magic line:

 ((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged(); 

Put this in your receiver of preferences, then you will be fine.

0
source

All Articles