isChecked vs. isSelected for Android CheckBoxes and RadioButtons

Brian Terczynski
3 min readDec 12, 2021

--

Just wanted to point out a mistake I’ve sometimes made. When working with CheckBoxes and RadioButtons, I’ve often made the mistake of calling isSelected() on them if I want to know whether its button has been “checked” or “pushed in”.

// I'm thinking this actually checks that the radio button is
// pushed in, but THIS IS ACTUALLY WRONG.
if (myRadioButton.isSelected()) {
// do something
}

I would blissfully think this is the correct code. But then things would not work right because even when the button was “checked”, isSelected() returned false. It’s not a compiler issue, nor does it throw an exception, because isSelected() is a perfectly valid function call. Instead, it would just result in a bug (and, if I failed to unit test it, that bug would get into production).

Of course the correct way to get the “checked” state of a Radio Button or a CheckBox is to use isChecked(). This is what will tell you if the button has been “checked”. The isChecked() method is defined in the CompoundButton superclass.

isSelected() is defined in the top-level View class. Its meaning? Well…it’s really up to the View subclass that utilizes this state. But one clue to how to think about it comes from the description of a StateListDrawable, and in particular the different state attributes you can use for the different <item> elements. In there, it describes the selected state as follows:

“true” if this item should be used when the object is the current user selection when navigating with a directional control (such as when navigating through a list with a d-pad); “false” if this item should be used when the object is not selected.

The selected state is used when focus (android:state_focused) is not sufficient (such as when list view has focus and an item within it is selected with a d-pad).

It also describes what the checked state means:

“true” if this item should be used when the object is checked; “false” if it should be used when the object is un-checked.

I guess the key here is where the description says “d-pad” (directional pad), and how the use of that indicates if an item is selected. And the fact that in this case focus isn’t enough because the ListView itself has the focus, but you navigate through the items with the d-pad and the current item in the navigation is “selected”. The isSelected() state may also have accessibility implications (like for screen readers).

Anyways . . . I generally have not had much need for isSelected() in the coding I have been doing; as I said the times I have used it were when I mistook it for isChecked(). The point being that, if you want to know if your Radio Button is pushed in, or your Check Box is checked . . . or similar states for other CompoundButtons (like a Switch or ToggleButton) use isChecked().

--

--

Brian Terczynski
Brian Terczynski

Written by Brian Terczynski

Documenting my learnings on my journey as a software engineer.

No responses yet