androidpreferencesselectedlistpreference

How get selected option from ListPreference?


I have ListPreference and it contains for example 5 options and I want to save one of this value to SharedPreferences when user selects it. How can I do it?

btw. I know how to save value to SharedPreferences, but I don't know how to get that value when user selects one of them.


Solution

  • OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {    
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // newValue is the value you choose
            return true;
        }
    };
    
    listPreference.setOnPreferenceChangeListener(listener);