6

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.

Mano
  • 517
  • 1
  • 8
  • 21
  • 1
    it has a defaultValue of false. and you are again changing it to false. How do you expect to see a change? – Mukesh Soni Sep 14 '12 at 11:44
  • I use the code of my commit inside an if where the user has previously changed the value to true – Mano Sep 14 '12 at 11:48

2 Answers2

15
CheckBoxPreference showContact = (CheckBoxPreference)findPreference("myPreference");
showContact.setChecked(false);
BamsBamx
  • 4,139
  • 4
  • 38
  • 63
Mano
  • 517
  • 1
  • 8
  • 21
2

You can call this in your preference activity

    CheckBoxPreference pref = (CheckBoxPreference)findPreference("example_pref_key");
    pref.setChecked(false);
mboy
  • 744
  • 7
  • 17