Reputation: 79
I have a checkbox preference and I want to set it's value from another checkbox. Now I can get the value from the checkboxpreference and set it to the external checkbox but I didn't know how to set the preference value from that external checkbox.Any Help PleaseThis is my code
getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); musicpref = getPrefs.getBoolean("musicp", true);
CheckBox musiccheck = (CheckBox) findViewById(R.id.checkBox1);
//The external Checkbox, is checked will start service , if not will stop it
musiccheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Intent musicstart = new Intent(MainActivity.this,
ServiceMusic.class);
startService(musicstart);
} else {
Intent musicstart = new Intent(MainActivity.this,
ServiceMusic.class);
stopService(musicstart);
}
}
});
// Preference checkbox , if is true will set the external checkbox to true
if (musicpref == true) {
musiccheck.setChecked(true);
}else{
musiccheck.setChecked(false);
}
Upvotes: 1
Views: 2497
Reputation: 5803
You could try something like this :
SharedPreferences preferendecs = PreferenceManager.getDefaultSharedPreferences(getContext());
Editor editor = preferencess.edit();
editor.putBoolean(PREF_NAME, musiccheck.getValue());
editor.commit();
You could also refer to this :
http://android-coding.blogspot.in/2011/08/example-of-using-checkboxpreference.html and
How do I capture changes of a CheckBoxPreference in android development?
Upvotes: 4