user577680 user577680 - 11 months ago 86
Android Question

mycheckbox.setSelected(false) has no effect

I have columns of checkboxes, the top row of which are CheckAll checkboxes for that particular column. If I uncheck the Checkall from the first CheckAll checkbox in the leftmost column I would like to uncheck the remaining CheckAll checkboxes.

However the

has no effect. If however, I do a
(just as a test) it DOES work and the checkbox is disabled.

By the way, this is a "header row" for a listview with a custom adapter. The contents of the listview work as expected.

Any idea how to get the checkbox unchecked?


You should use mycheckbox.setChecked(false) instead of setSelected.

I've tried searching for setSelected to see what it does, but in official documentation of the CheckBox, I was not able to find it (which suggested to me that this method is probably found in one of the parent classes of CheckBox). Tried typing it in Android Studio:

CheckBox cb = new CheckBox(getApplication());

Went to the implementation of the method (CTRL+Click) and saw this in the TextView class, from which almost every other widget is derived:

public void setSelected(boolean selected) {
    boolean wasSelected = isSelected();


    if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) {
        if (selected) {
        } else {

Interesting thing to note here is that setChecked method is contained in the CompoundButton class, while setSelected is is TextView. That means that setSelected does something completely different because textView surely cannot be checked/unchecked.

I hope this explains it well.