Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Value of a CheckBoxPreference in an Activity

Hello I need to know how to set a value programmatically.

I am using that code

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                            .
                            .
                            .

SharedPreferences.Editor geted = prefs.edit();
geted.putBoolean("checkBox_Schedule", false);
geted.commit();

But I dont see anything change

The code of my xml for my checkboxPreference is

 <CheckBoxPreference

 android:defaultValue="false"
 android:dependency="checkBox"
 android:key="checkBox_Schedule"
 android:summary="On/Off"
 android:title="Schedule" />

One solution is to do

 startActivity(new Intent(SetPreference.this, SetPreference.class));

But this is not what I want to do.

like image 715
Mano Avatar asked Sep 14 '12 11:09

Mano


2 Answers

CheckBoxPreference showContact = (CheckBoxPreference)findPreference("myPreference");
showContact.setChecked(false);
like image 91
Mano Avatar answered Oct 16 '22 15:10

Mano


You can call this in your preference activity

    CheckBoxPreference pref = (CheckBoxPreference)findPreference("example_pref_key");
    pref.setChecked(false);
like image 23
mboy Avatar answered Oct 16 '22 13:10

mboy