5

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 can see my log message in LogCat and it shows it as false when I click on the checkbox but its state doesn't change. It remains unchecked.

3 Answers 3

10

Why would you try to overwrite the default behavior with something like the default behavior? The checkbox toggles automatically on every click.

If you want to react on that, use the OnCheckedChangeListener.

Sign up to request clarification or add additional context in comments.

3 Comments

So it does... I just copied the code from a tutorial and it came with the onClick =.= so I just went with it. But yeah, it can check now, thanks. But aside from that, do you know why it didn't work the way in the question?
it's because you are toggling twice. once in your code, once in checkbox' code.
i made the same mistake as the op. now i know why code wasn't working. ha ha
4

To Make CheckBox checked or unchecked you can also use like

box.setChecked(true);

box.setChecked(false);

and to get state of CheckBox

if(box.isChecked()) {

  //do something here...

} else {

  //do something here...

}

Comments

0

for MaterialCheckBox, use checkedState instead of setChecked.

setFalse:

checkbox.checkedState = STATE_UNCHECKED

setTrue:

checkbox.checkedState = STATE_CHECKED

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.