Adams
Adams

Reputation: 115

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

Upvotes: 0

Views: 38

Answers (2)

Green goblin
Green goblin

Reputation: 9994

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);

Upvotes: 1

NIRMAL TANDEL
NIRMAL TANDEL

Reputation: 47

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:drawable="@drawable/layer176"
            android:state_checked="false"/>
        <item android:drawable="@drawable/checked250"
            android:state_checked="true"/>
        <item android:drawable="@drawable/layer176"/>

</selector>


========================================================

<CheckBox
        android:id="@+id/slider_listview_row_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:button="@drawable/checkbox_selector"
        android:background="@null"
        />


You can use above xml code ..

Upvotes: 0

Related Questions