The basic button class in Android is android.widget.Button. There’s not much to this
type of button, beyond how you use it to handle click events (see Listing 4–9).
Listing 4–9. Handling Click Events on a Button <Button android:id="@+id/ccbtn1" android:text="@+string/basicBtnLabel" android:typeface="serif" android:textStyle="bold" android:layout_width="fill_parent" android:layout_height="wrap_content" /> Button btn = (Button)this.findViewById(R.id.ccbtn1); btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
Intent intent = getButtonIntent(); intent.setAction("some intent data"); setResult(RESULT_OK, intent); finish();
} });
Listing 4–9 shows how to register for a button-click event. You register for the on-click event by calling the setOnClickListener() method with an OnClickListener. In Listing 4–9, an anonymous listener is created on the fly to handle click events for btn. When the button is clicked, the onClick() method of the listener is called.
Since Android SDK 1.6, there is an easier way to set up a click handler for your button or buttons. In the XML for a Button, you specify an attribute like this:
android:onClick="myClickHandler"
with a corresponding button handler method in your activity class like this: public void myClickHandler(View target) {
switch(target.getId()) { case R.id.ccbtn1: …
The handler method is called with target set to the View object representing the button that was pressed. Notice how the switch statement in the click handler method uses the resource IDs of the buttons to select the logic to run. Using this method means you won’t have to explicitly create each Button object in your code, and you can reuse the same method across multiple buttons; in general, it makes things easier to understand and maintain. This works with the other button types as well.
The ImageButton Control
Android provides an image button via android.widget.ImageButton. Using an image button is similar to using the basic button (see Listing 4–10).
Listing 4–10. Using an ImageButton
<ImageButton android:id="@+id/imageBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" />
ImageButton btn = (ImageButton)this.findViewById(R.id.imageBtn); btn.setImageResource(R.drawable.icon);
You can set the button’s image dynamically by calling setImageResource() or modifying the XML layout file (by setting the android:src property to the image ID), as shown in Listing 4–11.
Listing 4–11. Setting the ImageButton Image via XML <ImageButton android:id="@+id/imageBtn" android:src="@drawable/btnImage" android:layout_width="wrap_content" android:layout_height="wrap_content" />
The ToggleButton Control
The ToggleButton, like a check box or a radio button, is a two-state button. This button
can be in either the On state or the Off state. As shown in Figure 4–3, the ToggleButton’s
default behavior is to show a green bar when in the On state, and a grayed-out bar when in the Off state. Moreover, the default behavior also sets the button’s text to “On” when it’s in the On state and “Off” when it’s in the Off state.
Listing 4–12 shows an example.
Listing 4–12. The Android ToggleButton
<ToggleButton android:id="@+id/cctglBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toggle Button"/>
You can modify the text for the ToggleButton if On/Off is not appropriate for your
application. For example, if you have a background process that you want to start and stop via a ToggleButton, you could set the button’s text to Run and Stop by using android:textOn and android:textOff properties (see Listing 4–13). Because
ToggleButtons have on and off text as separate attributes, the android:text attribute of
a ToggleButton is not really used. It’s available because it has been inherited (from TextView, actually), but in this case you don’t need to use it.
Listing 4–13. Setting the ToggleButton’s Label <ToggleButton android:id="@+id/cctglBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Run" android:textOff="Stop" android:text="Toggle Button"/>
The CheckBox Control
A check-box control plays a part in virtually all widget toolkits. HTML, JFC, and JSF all support the concept of a check box. The check-box control is a two-state button that allows the user to toggle its state.
In Android, you can create a check box by creating an instance of
android.widget.CheckBox. See Listing 4–14 and Figure 4–4. Listing 4–14. Creating Check Boxes
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<CheckBox android:text="Chicken" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:text="Fish" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:text="Steak" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Figure 4–4. Using the CheckBox control
You manage the state of a check box by calling setChecked() or toggle(). You can
obtain the state by calling isChecked().
If you need to implement specific logic when a check box is checked or unchecked, you can register for the on-checked event by calling setOnCheckedChangeListener() with an
implementation of the OnCheckedChangeListener interface. You’ll then have to implement
the onCheckedChanged() method, which will be called when the check box is checked or
unchecked.
The RadioButton 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:id="@+id/rBtnGrp" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" >
<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, although you can set one to checked in the XML definition. To set one of the radio buttons to the checked state programmatically, you can obtain a reference to the radio
button 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
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" 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);
Finally, once a user has checked a radio button within a radio group, the user cannot uncheck it by clicking it again. The only way to clear all radio buttons within a radio