Skip to content

Instantly share code, notes, and snippets.

@ET-CS
Last active October 22, 2018 16:17
Show Gist options
  • Save ET-CS/5326611 to your computer and use it in GitHub Desktop.
Save ET-CS/5326611 to your computer and use it in GitHub Desktop.
How to implement TwoState using CheckBoxPreference / android development
public class PreferencesActivity extends PreferenceActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// find the CheckBox
CheckBoxPreference chkbox = (CheckBoxPreference) findPreference(getString(R.string.chkboxid));
// set OnClick listener
chkbox.setOnPreferenceClickListener(chkboxListener);
// update status
if (chkbox.isChecked() & chkbox.isEnabled()) {
setStatus((Preference) findPreference(getString(R.string.pref2)), false);
}
...
}
// disable or enable preference
private void setStatus(Preference pref, boolean stats) {
pref.setEnabled(stats);
}
// Lisener for onclick on seperate ringtone checkbox to disable notification sound checkbox
private OnPreferenceClickListener chkboxListener = new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
CheckBoxPreference cb = (CheckBoxPreference) preference;
setStatus((Preference) findPreference(getString(R.string.pref2)), !cb.isChecked());
return true;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment