The Radio Button Control

Radio-button controls are an integral part of any UI toolkit. A radio button gives the user several choices and forces her to select a single item. To enforce this single-selection model, radio buttons generally belong to a group and each group is forced to have only one item selected at a time.

To create a group of radio buttons in Android, first create a RadioGroup and then populate the group with radio buttons. Listing 4-15 and Figure 4-5 show an example.

Listing 4-15. Using Android Radio-Button Widgets

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<RadioGroup android:layout_width="wrap_content"

android:layout_height="wrap_content">

<RadioButton android:id="@+id/chRBtn"

android:text="Chicken" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<RadioButton android:id="@+id/fishRBtn" android:text="Fish" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<RadioButton android:id="@+id/stkRBtn" android:text="Steak" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

</RadioGroup> </LinearLayout>

In Android, you implement a radio group using android.widget.RadioGroup and a radio button using android.widget.RadioButton.

Figure 4-5. Using radio buttons

Note that the radio buttons within the radio group are, by default, unchecked to begin with. To set one of the radio buttons to the checked state, you can obtain a reference to the radio button programmatically and call setChecked():

RadioButton rbtn = (RadioButton)this.findViewById(R.id.stkRBtn); rbtn.setChecked(true);

You can also use the toggle() method to toggle the state of the radio button. As with the CheckBox control, you will be notified of on-checked or on-unchecked events if you call the setOnCheckedChangeListener() with an implementation of the OnCheckedChangeListener interface.

Realize that RadioGroup can also contain views other than the radio button. For example, Listing 4-16 adds a TextView after the last radio button. Also note that a radio button lies outside the radio group.

Listing 4-16. A Radio Group with More Than Just Radio Buttons

<LinearLayout 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/anotherRadBtn"

Figure 4-5. Using radio buttons android:text="Outside"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

<RadioGroup android:id="@+id/rdGrp"

android:layout_width="wrap_content" android:layout_height="wrap_content">

<RadioButton android:id="@+id/chRBtn" android:text="Chicken" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/fishRBtn" android:text="Fish" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/stkRBtn" android:text="Steak" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<TextView android:text="My Favorite"

android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RadioGroup>

</LinearLayout>

Listing 4-16 shows that you can have non-RadioButton controls inside a radio group. Moreover, you should know that the radio group can enforce single-selection only on the radio buttons within its own container. That is, the radio button with ID anotherRadBtn will not be affected by the radio group shown in Listing 4-16 because it is not one of the group's children.

Also know that you can manipulate the RadioGroup programmatically. For example, you can obtain a reference to a radio group programmatically and add a radio button (or other type of control):

RadioGroup rdgrp = (RadioGroup)findViewById(R.id.rdGrp); RadioButton newRadioBtn = new RadioButton(this); newRadioBtn.setText("Pork"); rdgrp.addView(newRadioBtn);

0 0

Post a comment