I have a Preferences Activity in my code. I want to show a toast when ever user change radio button in a ListPreferences. I used this code but it does not work. :(
public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {
ListPreference listPreference = (ListPreference) findPreference(key);
listPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
Toast.makeText(PrefsActivity.this, "second", Toast.LENGTH_LONG).show();
return false;
}
});
}
}
what is my problem?
Change your onCreate
code to:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
}
You are not adding the registerOnSharedPreferenceChangeListener
to PrefsActivity
Activity.