The Toggle Button 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).

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

0 0

Post a comment