Skip to content

Most visited

Recently visited



In this document

  1. Responding to Click Events

Key classes

  1. CheckBox

Checkboxes allow the user to select one or more options from a set. Typically, you should present each checkbox option in a vertical list.

To create each checkbox option, create a CheckBox in your layout. Because a set of checkbox options allows the user to select multiple items, each checkbox is managed separately and you must register a click listener for each one.

Responding to Click Events

When the user selects a checkbox, the CheckBox object receives an on-click event.

To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

For example, here are a couple CheckBox objects in a list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    <CheckBox android:id="@+id/checkbox_meat"
    <CheckBox android:id="@+id/checkbox_cheese"

Within the Activity that hosts this layout, the following method handles the click event for both checkboxes:

public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
            if (checked)
                // Put some meat on the sandwich
                // Remove the meat
            if (checked)
                // Cheese me
                // I'm lactose intolerant
        // TODO: Veggie sandwich

The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:

Tip: If you need to change the checkbox state yourself (such as when loading a saved CheckBoxPreference), use the setChecked(boolean) or toggle() method.

Additional sample code

To download a sample about creating a custom single or multichoice ListView UI, see Android BasicAccessibility Sample.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)