-1

My question is simple i want to checked checkbox for always even app is closed and relaunch. This process should continue till user itself uncheck it. If user itself uncheck it then should be uncheck till user checked it again in android .

Please guide me how can I do this.

MajidTiger
  • 17
  • 5
  • `public boolean isCheckBoxCheked=true;`. Default `checkboxObj.setEnable(isCheckBoxCheked);`. save the value in [SharedPreferance](https://developer.android.com/reference/android/preference/PreferenceActivity). when user uncheck the checkbox. make it `false` and again save it. Thanks Peace. – Empty Brain Jul 05 '18 at 04:31
  • use PreferenceActivity. https://developer.android.com/reference/android/preference/PreferenceActivity – Vinay Rathod Jul 05 '18 at 04:32
  • check it -https://stackoverflow.com/questions/8501342/how-to-show-pre-checked-checkboxes-in-android – Mr. Ad Jul 05 '18 at 04:44

4 Answers4

0

For that you have to use SharedPreferance globally to store the default value of check box and change value inside the preference onCheckChange and when view load set state of checkbox according to flag in preferance

Jay Thummar
  • 2,281
  • 1
  • 14
  • 22
0

keep a boolean flag in sharedPreferences or localDB for the user action. And by default keep this flag's value as true. When the user clicks checkbox, update this flag's value as well. So, whenever the application is launched, check this flag's value like:-

if(checkBoxEnabled) {
     checkBox.setEnabled(true);
} else {
     checkBox.setEnabled(false);
}
0

Where you have added check box in xml layout, add android:checked="true" for initial check.

Add status to SharedPreferences when you listen a change in value. e.g. onCheckedChanged()

SharedPreferences sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);

sp.edit.putBoolean("check_status", checkBox.isChecked()).apply();
0

Use this one simple code ......

Use SharedPreferences to maintain state of check boxes.

Initialization....

checkBox.setChecked(getSharedPreferences("MyAPP", Context.MODE_PRIVATE).getBoolean("checkBox", true));

OnCheckedChangeListener

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         getSharedPreferences("MyAPP", Context.MODE_PRIVATE).edit().putBoolean("checkBox", isChecked).commit();

              }
        });

First time all check boxes will be checked and it will be work like you want....

Above code only for one check box. Try same for other check-boxes. Just change unique key for all check boxes like i used here "checkbox" in checkBox.setChecked(getSharedPreferences("MyAPP", Context.MODE_PRIVATE).getBoolean("checkBox", true));

hope your work done by this.....

sushildlh
  • 8,986
  • 4
  • 33
  • 77