I have some CheckBoxPreference elements for which I would like to use one onClickListener for all the CheckBoxPreference in my page. Here is what I'm trying to do:
CheckBoxPreference checkboxPref = (CheckBoxPreference)getPreferenceManager().findPreference("preference1");
CheckBoxPreference checkboxPref1 = (CheckBoxPreference)getPreferenceManager().findPreference("preference2");
checkboxPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
CheckBoxPreference pref = (CheckBoxPreference) findPreference("preference1");
if (pref.isChecked())
{
pref.setChecked(false);
dialog.show();
}else if (!pref.isChecked())
{
pref.setChecked(true);
dialog.show();
}
return false;
}
});
}
How can i make this onPreferenceClick() be executed for the checkboxPref1? Any ideas? Thanks!
OnePreferenceClickListener listener = new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
CheckBoxPreference pref = (CheckBoxPreference) findPreference("preference1");
pref.setChecked(!pref.isChecked());
dialog.show();
return false;
}
});
checkboxPref.setOnPreferenceClickListener(listener);
checkboxPref1.setOnPreferenceClickListener(listener);
Alternatively you can have your class implement the interface and then just pass this to both checkbox preferences.