Last active
October 22, 2018 16:17
-
-
Save ET-CS/5326611 to your computer and use it in GitHub Desktop.
How to implement TwoState using CheckBoxPreference / android development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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