user577680 user577680 - 5 months ago 34
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

mycheckbox.setSelected(false)
has no effect. If however, I do a
mycheckbox.setEnabled(false)
(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?

Answer

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());
cb.setSelected(true);

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

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

    super.setSelected(selected);

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

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.