Check Boxes and Radio buttons are another set of basic widgets for presenting data and choices to the user of your android application. Both checkboxes and radio buttons have two states: checked and unchecked. The difference is, checkboxes are standalone elements that can be toggled between checked and unchecked states whereas radio buttons once checked cannot be unchecked. Also multiple radio buttons can be part of a radio group.
Both CheckBox and RadioButton inherit from CompoundButton which has methods to the view/manipulate the state of the button:
- toggle(): Change the state to its inverse state
- setChecked(boolean state): Change to a specific state.
- ischecked(): Check the current state .
In addition, you can register a callback method to be executed whenever the state of the button changes via the setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) method.
RadioGroup extends the LinearLayout and contains multiple radio buttons whose states depend on each other. Within a radio group, only one radio-button can have its state as checked at any point in time. RadioGroup also provides methods to manipulate the state of the radio buttons embedded within it. Once you assign id to the RadioGroup and its buttons in the xml layout file, you can use it to:
- check(int id): Set the selection to a specific radio button
- clearCheck(): Clears all the selection so that the none of the radio buttons are selected in the group.
- getCheckedRadioButtonID(): Fetches the id of the radio button whose state is currently checked within the group. If none are selected, returns -1.
RadioGroup also provides a callback interface that can be registered via the setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener) method. Note that the OnCheckedChangeListener interface for RadioGroup and the CompoundButton are different as highlighted.
Lets look at the usage with a example. The desired output and the corresponding XML layout files are shown.
- We use a linear layout to arrange the elements on the screen. We start with a checkbox with a default text.
- We then place a standalone radio button.
- We use a TextView element to display the selection done in the RadioGroup.
- Finally, we use the RadioGroup element and position two radio buttons within it. Note that we assign ids to all the elements in the layout file so that we can manipulate their states in the Java code.
- We get the reference to the checkbox using the findViewById() method and register a call back to update the text of the Checkbox depending on its state using the setOnCheckedChangedListner() method.
- We do the same thing with the radio button, but this time with the setOnclickListener() method. As mentioned above radio buttons once checked cannot be unchecked from the UI. Note that both CheckBox and RadioButton inherit from the CompoundButton which in turn inherits from TextView. So all the properties available on the TextView element can also be used to manipulate the text of these elements as well.
- Finally, we register another callback to the RadioGroup element which updates the TextView element’s text based on the selection done in the RadioGroup. As highlighted below, note the difference in the type of OnCheckedChangeListener() interface that is passed as arguments the callbacks.