Recipe Using Check Boxes and Toggle Buttons

Check boxes have a predetermined checkmark graphic, colors for selection, and colors for behavior when pressed.This provides a unifying look and feel across Android applications. However, if a custom graphic to denote selection is required, the setButtonDrawable() method can be used.

Sticking with the check box example here, the CheckBox widget needs to be declared in a layout file, as shown in Listing 4.16.The android:text attribute displays as a label after the checkbox. For illustration, a few text views are also added to the layout.

Listing 4.16 res/layout/ckbox.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=""



android:layout_height="fill_parent"> <CheckBox android:id="@+id/checkbox0" android:text="Lettuce" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/checkbox1" android:text="Tomato" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/checkbox2" android:text="Cheese" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="Lettuce, Tomato, Cheese choices:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/status"

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

The views in the layout file can be associated with view instances in the Java file, as shown in Listing 4.17. Here, a private inner class is used to register the toppings of a sand-wich.All three checkboxes have an onClickListener, which keeps track of the changes to the toppings, and this is updated to the text view as an example. The final output with some sample selections is shown in Figure 4.12.

Listing 4.17 src/com/cookbook/layout_widgets/ package com.cookbook.layout_widgets;


import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.CheckBox;

import android.widget.TextView;

public class CheckBoxExample extends Activity { private TextView tv;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ckbox); tv = (TextView) findViewById(;

class Toppings {private boolean LETTUCE, TOMATO, CHEESE;}

final Toppings sandwichToppings = new Toppings(); final CheckBox checkbox[] = {

(CheckBox) findViewById(,

(CheckBox) findViewById(, (CheckBox) findViewById(};

checkbox[0].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {

sandwichToppings.LETTUCE = true; } else {

sandwichToppings.LETTUCE = false;

tv.setText(""+sandwichToppings.LETTUCE + " " +sandwichToppings.TOMATO + " " +sandwichToppings.CHEESE + " ");

checkbox[1].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {

sandwichToppings.TOMATO = true; } else {

sandwichToppings.TOMATO = false;

tv.setText(""+sandwichToppings.LETTUCE + " " +sandwichToppings.TOMATO + " " +sandwichToppings.CHEESE + " ");

checkbox[2].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {

sandwichToppings.CHEESE = true; } else {

sandwichToppings.CHEESE = false;

tv.setText(""+sandwichToppings.LETTUCE + " " +sandwichToppings.TOMATO + " " +sandwichToppings.CHEESE + " ");


1 Lettuce

J Tomato

J Cheese

Choices: false Irue true

Figure 4.12 Checkbox example showing unselected and selected widgets.

Toggle buttons are similar to checkboxes, but use a different graphic. In addition, the text is incorporated into the button rather than put alongside. Listing 4.16 (and Listing 4.17 for that matter) can be modified to replace each CheckBox with a ToggleButton:

<ToggleButton android:id="@+id/ToggleButton0" android:textOff="No Lettuce" android:textOn="Lettuce" android:layout_width="wrap_content" android:layout_height="wrap_content" />

Note the android:text element is replaced by an android:textOff (defaults to "OFF" if not specified) and android:textOn (defaults to "ON" if not specified) element for display depending on the selection state of the toggle button.An example output is shown in Figure 4.13.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment