Checkbox Help

  • Replies:3
Shaun Norton
  • Forum posts: 1

Sep 21, 2012, 10:35:55 AM via Website

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.

Reply
ISA Nexus
  • Forum posts: 21

Dec 27, 2012, 7:36:43 PM via Website

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

duc nguyen

Reply
duc nguyen
  • Forum posts: 4

Jan 6, 2013, 8:41:46 AM via Website

Read more preferrence in android here
developer.android.com/reference/android/preference/package-summary.html

Reply
Ashish Tripathi
  • Forum posts: 211

Feb 4, 2013, 12:37:21 PM via Website

Shared preference is like a database to store the preferences. Go through the tutorial.

if any problem send the code.

Reply