1

Ya there are plenty of questions and answers about this stuff in the net but i just can't figure out how to save the checkbox state using sharedpreference. Someone just help me with the coding part which i was not able to do.

ch = (CheckBox) findViewById(R.id.checkBox1);

    ch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        if(ch.isChecked())
                {
            Toast.makeText(getApplicationContext(), "Check", Toast.LENGTH_SHORT).show();
            }
        else
        {
            Toast.makeText(getApplicationContext(), "Uncheck", Toast.LENGTH_SHORT).show();
        }}
    });
    }
kumareloaded
  • 3,882
  • 14
  • 41
  • 58

3 Answers3

5

Just create SharedPrefrences and Add value using .putBoolean()

 if(ch.isChecked()){             

    SharedPreferences settings = getSharedPreferences(PREFRENCES_NAME, 0);
    settings.edit().putBoolean("check",true).commit();

}
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
2

I think answer you already knew, let me clear your way,

  • make a boolean field in table
  • set its value true when CheckBox is checked else false

Next time when you are loading the activity, just read the field value and set Checkbox's status accordingly.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
1
if(ch.isChecked())
{
    SharedPreferences preferences = context.getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
    preferences.edit().putBoolean("checked", True).commit();
}
Samer
  • 536
  • 3
  • 5