androidcheckboxsharedpreferencescheckboxpreference

change CheckBoxPreferesses if normal checkbox is chacked


I'm working on an android application, and i have in my signup layout a checkbox for a value that can be edited from my settings (settings are sharedpreferences) so when the user signup can check that checkbox and if this checkbox is checked so the one in my settings (CheckBoxPreferences), my question is how can i change CheckBoxPreferences value depending on if the checkbox from the signup activity is checked?

thank you


Solution

  • Store CheckBox value in SharedPreferncesand based on value ofSharedPreferences, setCheckBoxPreference` to check/uncheck

    boolean isCheckBoxChecked = <GetCheckBoxState>;
    SharedPreferences prefs = getSharedPreferences("PrefsName", <Mode>);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("<Key>", isCheckBoxChecked).commit();
    
    //Retrieve SharedPreferences value and set it to CheckBoxPreference
    boolean value = prefs.getBoolean("<Key>", <DefaultValue>);
    CheckBoxPreference cbPref = (CheckBoxPreference) findPreference("<YourPreference>");
    cbPref.setChecked(value);