Code example for CheckBox

Methods: getContextisCheckedsetCheckedsetOnCheckedChangeListener

     * we will care for persistence - means on checked change there happens some save and on init set the state 
     * @param tag - setting is bound to tag - same tag same setting 
    public void careForCheckedStatePersistence(final String tag) {
        final SharedPreferences mSharedPrefs = checkBox.getContext().getSharedPreferences("ui_state", Context.MODE_WORLD_READABLE);
        checkBox.setChecked(mSharedPrefs.getBoolean(tag, checkBox.isChecked()));
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSharedPrefs.edit().putBoolean(tag, isChecked).commit();
Experience pair programming with AI  Get Codota for Java