views:

471

answers:

2

I know that I can do something like this:

Preference pref = findPreference(getString(R.string.pref_vibrate_on_key));
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            LogUtil.d("Working!");
            return true;
        }

});

But I would like to add a Listener to every preference. I tried doing:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(
                SharedPreferences sharedPreferences, String key) {
            LogUtil.d("working!");

        }

});

but it doesn't work. Is this possible? If so, what am I doing wrong?

+1  A: 

I think that onSharedPrefererenceChanged is fired od saving the preference (when you click BACK or HOME in PreferenceActivity). I think that the easiest way is to create single class implementing OnPreferenceChangeListener and switch through Preference.getKey(); and set it as OnPreferenceChangeListener for each Preference.

skyman
+1  A: 

I found a workaround using Reto Meier answer at http://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-sum

Using his approach it does work.

PS: Thanks Reto :)

Macarse