The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Other Common Widgets and Containers

In the chapter on basic widgets, we left out all of the classic “two-state” widgets, such as checkboxes and radio buttons. We will examine those and other related widgets in this chapter.

Beyond the classic general-purpose containers (LinearLayout, RelativeLayout, TableLayout), there are other specialized containers, like FrameLayout and RadioGroup, that you will use from time to time. We will examine those in this chapter as well.

Just a Box to Check

The classic checkbox has two states: checked and unchecked. Clicking the checkbox toggles between those states to indicate a choice (e.g., “Add rush delivery to my order”).

In Android, there is a CheckBox widget to meet this need. It has TextView as an ancestor, so you can use TextView properties like android:textColor to format the widget.

Within Java, you can invoke:

  1. isChecked() to determine if the checkbox has been checked
  2. setChecked() to force the checkbox into a checked or unchecked state
  3. toggle() to toggle the checkbox as if the user clicked upon it

Also, you can register a listener object (in this case, an instance of OnCheckedChangeListener) to be notified when the state of the checkbox changes.

For example, from the Basic/CheckBox sample project, here is a simple checkbox layout:

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/check"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/unchecked"/>

The corresponding CheckBoxDemo.java retrieves and configures the behavior of the checkbox:

package com.commonsware.android.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class CheckBoxDemo extends Activity implements
    CompoundButton.OnCheckedChangeListener {
  CheckBox cb;

  @Override
  public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.main);

    cb=(CheckBox)findViewById(R.id.check);
    cb.setOnCheckedChangeListener(this);
  }

  public void onCheckedChanged(CompoundButton buttonView,
                               boolean isChecked) {
    if (isChecked) {
      cb.setText(R.string.checked);
    }
    else {
      cb.setText(R.string.unchecked);
    }
  }
}

Note that the activity serves as its own listener for checkbox state changes since it implements the OnCheckedChangeListener interface (set via cb.setOnCheckedChangeListener(this)). The callback for the listener is onCheckedChanged(), which receives the checkbox whose state has changed and what the new state is. In this case, we update the text of the checkbox to reflect what the actual box contains.

CheckBoxDemo Sample App, with CheckBox Checked
Figure 139: CheckBoxDemo Sample App, with CheckBox Checked

Android Studio Graphical Layout Editor

The CheckBox widget can be found in the “Buttons” portion of the Palette in the Android Studio Graphical Layout editor:

Widgets Palette, CheckBox Shown Highlighted
Figure 140: Widgets Palette, CheckBox Shown Highlighted

You can drag it into the layout and configure it as desired using the Attributes pane. As CheckBox inherits from TextView, most of the settings are the same as those you would find on a regular TextView.

Don’t Like Checkboxes? How About Toggles or Switches?

The preview of this section was abducted by space aliens.

Turn the Radio Up

The preview of this section was abducted by space aliens.

Scrollwork

The preview of this section is in the process of being translated from its native Klingon.

Making Progress with ProgressBars

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

Framing the Scene

The preview of this section is in the process of being translated from its native Klingon.

Visit the Trails!

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.