Checkbox Help

  • Replies:3
  • OpenNot stickiedUnanswered

Sep 21, 2012 10:35:55 AM

I need help with saving the state of a Checkbox.

I want to select a checkbox, then close the app and when i open the app again, i want the checkbox to still be selected.

Please can somebody help with this or at least point me in the right direction.

Dec 27, 2012 7:36:43 PM

Your save the value in the persistent preferences when clicked. And you read its value when populating from layout

SharedPreferences settings = getSharedPreferences("preferences", 0);
final CheckBox checkBox = (CheckBox)findViewById(R.id.yourCheckBox);
checkBox.setChecked(settings.getBoolean("checkBoxValue", false));
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences("preferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkBoxValue", checkBox.isChecked());
editor.commit();
}
});