150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpage.title=Checkboxes
264461bf4f261164cb9e3022761fd217fd0028ac5Scott Main
350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main@jd:body
450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv-wrapper">
650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv">
750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2>In this document</h2>
850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ol>
950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#HandlingEvents">Responding to Click Events</a></li>
1050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ol>
1150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
1250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2>Key classes</h2>
1350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ol>
1450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>{@link android.widget.CheckBox}</li>
1550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ol>
1650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
1750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
1850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
1950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Checkboxes allow the user to select one or more options from a set. Typically, you should
2050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpresent each checkbox option in a vertical list.</p>
2150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
2250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/checkboxes.png" alt="" />
2350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
2450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>To create each checkbox option, create a {@link android.widget.CheckBox} in your layout. Because
2550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maina set of checkbox options allows the user to select multiple items, each checkbox is managed
2650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainseparately and you must register a click listener for each one.</p>
2750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
2850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="HandlingEvents">Responding to Click Events</h2>
2950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>When the user selects a checkbox, the {@link android.widget.CheckBox} object receives an
3150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainon-click event.</p>
3250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>To define the click event handler for a checkbox, add the <code><a
3450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="/reference/android/R.attr.html#onClick">android:onClick</a></code> attribute to the
3550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<code>&lt;CheckBox&gt;</code> element in your XML
3650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlayout. The value for this attribute must be the name of the method you want to call in response
3750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainto a click event. The {@link android.app.Activity} hosting the layout must then implement the
3850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maincorresponding method.</p>
3950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>For example, here are a couple {@link android.widget.CheckBox} objects in a list:</p>
4150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
4350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;?xml version="1.0" encoding="utf-8"?>
4450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:orientation="vertical"
4650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_width="fill_parent"
4750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_height="fill_parent">
4850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;CheckBox android:id="&#64;+id/checkbox_meat"
4950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_width="wrap_content"
5050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_height="wrap_content"
5150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:text="&#64;string/meat"
5250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:onClick="onCheckboxClicked"/>
5350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;CheckBox android:id="&#64;+id/checkbox_cheese"
5450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_width="wrap_content"
5550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_height="wrap_content"
5650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:text="&#64;string/cheese"
5750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:onClick="onCheckboxClicked"/>
5850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;/LinearLayout>
5950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
6050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
6150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Within the {@link android.app.Activity} that hosts this layout, the following method handles the
6250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainclick event for both checkboxes:</p>
6350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
6450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
6550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpublic void onCheckboxClicked(View view) {
6650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    // Is the view now checked?
67fe3b1cb27dd7ded1358bf3c4cc047de97158ed1eScott Main    boolean checked = ((CheckBox) view).isChecked();
6850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    
6950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    // Check which checkbox was clicked
7050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    switch(view.getId()) {
7150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        case R.id.checkbox_meat:
7250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main            if (checked)
7350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main                // Put some meat on the sandwich
7450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main            else
7550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main                // Remove the meat
7650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main            break;
7750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        case R.id.checkbox_cheese:
7850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main            if (checked)
7950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main                // Cheese me
8050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main            else
8150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main                // I'm lactose intolerant
8250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main            break;
8350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        // TODO: Veggie sandwich
8450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    }
8550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main}
8650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
8750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
8850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>The method you declare in the {@link android.R.attr#onClick android:onClick} attribute
8950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmust have a signature exactly as shown above. Specifically, the method must:</p>
9050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ul>
9150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>Be public</li>
9250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>Return void</li>
9350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>Define a {@link android.view.View} as its only parameter (this will be the {@link
9450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.view.View} that was clicked)</li>
9550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ul>
9650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
97e440cde0c49e3b101fa86ba527939299d7ce5cc0David Friedman<p class="note"><strong>Tip:</strong> If you need to change the checkbox state
9850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainyourself (such as when loading a saved {@link android.preference.CheckBoxPreference}),
9950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainuse the {@link android.widget.CompoundButton#setChecked(boolean)} or {@link
10050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.CompoundButton#toggle()} method.</p>