I need to control a CheckBoxPreference manually; I have to check a condition in my own data to determine if the preference can be set or not.
How can I do it? My current code is as follows, but it doesn’t seem to work.
CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (something) {
return true; // checkbox should be checked
} else {
return false; // checkbox should be unchecked
}
Should I always return false and then use
buyPref.setChecked(true);
I think you want something like this:
You want to always return false from this so that Android doesn’t attempt to write the preference itself. See the documentation for
OnPreferenceChangeListenerfor more info.Note that all of this will happen on the UI thread, so if you need to do anything long-running, I would throw it in an
AsyncTaskwith aProgressDialogso that the user doesn’t get frustrated.