Android Development: Checkbox setChecked not working

In my xml:

<CheckBox android:id="@+id/checkboxUpdateLessonPlanAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chkLessonPlanAll"
            android:onClick="onCheckboxClicked"/>

In my java:

public void onCheckboxClicked(View view) {
    //CheckBox box = (CheckBox) view;
    CheckBox box = (CheckBox) findViewById(R.id.checkboxUpdateLessonPlanAll);
    box.setChecked(!box.isChecked());
    Log.v("qwerty", "checkbox clicked " + box.isChecked() + "!!");
}

I see my log message in LogCat and it shows it as false when I click on this flag, but its status does not change. It remains untested.

+4
source share
2 answers

Why are you trying to overwrite the default behavior with something like the default behavior? The checkbox automatically switches to every click.

If you want to respond to this, use OnCheckedChangeListener.

+8
source

To make CheckBoxmarked or unchecked, you can also use as

box.setChecked(true);

box.setChecked(false);

and get a fortune CheckBox

if(box.isChecked()) {

  //do something here...

} else {

  //do something here...

}
+3
source

All Articles