The result? Clicking the checkbox immediately updates its text, as you can see in Figures 6-4 and 6-5.

Figure 6-4.

Figure 6-5.
Turn the Radio Up
As with other implementations of radio buttons in other toolkits, Android’s radio buttons are two-state, like checkboxes, but can be grouped such that only one radio button in the group can be checked at any time.
Like CheckBox
, RadioButton
inherits from CompoundButton
, which in turn inherits from TextView
. Hence, all the standard TextView
properties for font face, style, color, etc., are available for controlling the look of radio buttons. Similarly, you can call isChecked()
on a RadioButton
to see if it is selected, toggle()
to select it, and so on, like you can with a CheckBox
.
Most times, you will want to put your RadioButton
widgets inside of a RadioGroup
. The RadioGroup
indicates a set of radio buttons whose state is tied, meaning only one button out of the group can be selected at any time. If you assign an android:id
to your RadioGroup
in your XML layout, you can access the group from your Java code and invoke:
• check()
to check a specific radio button via its ID (e.g., group.check (R.id.radio1)
)
• clearCheck()
to clear all radio buttons, so none in the group are checked
• getCheckedRadioButtonId()
to get the ID of the currently-checked radio button (or -1 if none are checked)
For example, from the Basic/RadioButton
sample application, here is an XML layout showing a RadioGroup
wrapping a set of RadioButton
widgets:
<?xml version='1.0' encoding='utf-8'?>
<RadioGroup
xmlns:android='http://schemas.android.com/apk/res/android'
android:orientation='vertical'
android:layout_width='fill_parent'
android:layout_height='fill_parent'>
<RadioButton android:id='@+id/radio1'
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:text='Rock' />
<RadioButton android:id='@+id/radio2'
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:text='Scissors' />
<RadioButton android:id='@+id/radio3'
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:text='Paper' />
</RadioGroup>
Figure 6-6 shows the result using the stock Android-generated Java for the project and this layout.

Figure 6-6.
Note that the radio button group is initially set to be completely unchecked at the outset. To pre-set one of the radio buttons to be checked, use either setChecked()
on the RadioButton
or check()
on the RadioGroup
from within your onCreate()
callback in your activity.
It’s Quite a View
All widgets, including the ones previously shown, extend View, and as such give all widgets an array of useful properties and methods beyond those already described.
Useful Properties
Some of the properties on View most likely to be used include:
• Controls the focus sequence:
• android:nextFocusDown
• android:nextFocusLeft
• android:nextFocusRight
• android:nextFocusUp
• android:visibility
, which controls whether the widget is initially visible
• android:background
, which typically provides an RGB color value (e.g., #00FF00
for green) to serve as the background for the widget
Useful Methods
You can toggle whether or not a widget is enabled via setEnabled()
and see if it is enabled via isEnabled()
. One common use pattern for this is to disable some widgets based on a CheckBox
or RadioButton
selection.
You can give a widget focus via requestFocus()
and see if it is focused via isFocused()
. You might use this in concert with disabling widgets as previously mentioned, to ensure the proper widget has the focus once your disabling operation is complete.
To help navigate the tree of widgets and containers that make up an activity’s overall view, you can use:
• getParent()
to find the parent widget or container
• findViewById()
to find a child widget with a certain ID
• getRootView()
to get the root of the tree (e.g., what you provided to the activity via setContentView()
)