1 page.title=Radio Buttons 2 page.tags="radiobutton","radiogroup" 3 @jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 <h2>In this document</h2> 8 <ol> 9 <li><a href="#HandlingEvents">Responding to Click Events</a></li> 10 </ol> 11 12 <h2>Key classes</h2> 13 <ol> 14 <li>{@link android.widget.RadioButton}</li> 15 <li>{@link android.widget.RadioGroup}</li> 16 </ol> 17 </div> 18 </div> 19 20 <p>Radio buttons allow the user to select one option from a set. You should use radio buttons for 21 optional sets that are mutually exclusive if you think that the user needs to see all available 22 options side-by-side. If it's not necessary to show all options side-by-side, use a <a 23 href="{@docRoot}guide/topics/ui/controls/spinner.html">spinner</a> instead.</p> 24 25 <img src="{@docRoot}images/ui/radiobuttons.png" alt="" /> 26 27 <p>To create each radio button option, create a {@link android.widget.RadioButton} in your layout. 28 However, because radio buttons are mutually exclusive, you must group them together inside a 29 {@link android.widget.RadioGroup}. By grouping them together, the system ensures that only one 30 radio button can be selected at a time.</p> 31 32 <h2 id="HandlingEvents">Responding to Click Events</h2> 33 34 <p>When the user selects one of the radio buttons, the corresponding {@link 35 android.widget.RadioButton} object receives an on-click event.</p> 36 37 <p>To define the click event handler for a button, add the <code><a 38 href="/reference/android/R.attr.html#onClick">android:onClick</a></code> attribute to the 39 <code><RadioButton></code> element in your XML 40 layout. The value for this attribute must be the name of the method you want to call in response 41 to a click event. The {@link android.app.Activity} hosting the layout must then implement the 42 corresponding method.</p> 43 44 <p>For example, here are a couple {@link android.widget.RadioButton} objects:</p> 45 46 <pre> 47 <?xml version="1.0" encoding="utf-8"?> 48 <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 49 android:layout_width="fill_parent" 50 android:layout_height="wrap_content" 51 android:orientation="vertical"> 52 <RadioButton android:id="@+id/radio_pirates" 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" 55 android:text="@string/pirates" 56 android:onClick="onRadioButtonClicked"/> 57 <RadioButton android:id="@+id/radio_ninjas" 58 android:layout_width="wrap_content" 59 android:layout_height="wrap_content" 60 android:text="@string/ninjas" 61 android:onClick="onRadioButtonClicked"/> 62 </RadioGroup> 63 </pre> 64 65 <p class="note"><strong>Note:</strong> The {@link android.widget.RadioGroup} is a subclass of 66 {@link android.widget.LinearLayout} that has a vertical orientation by default.</p> 67 68 <p>Within the {@link android.app.Activity} that hosts this layout, the following method handles the 69 click event for both radio buttons:</p> 70 71 <pre> 72 public void onRadioButtonClicked(View view) { 73 // Is the button now checked? 74 boolean checked = ((RadioButton) view).isChecked(); 75 76 // Check which radio button was clicked 77 switch(view.getId()) { 78 case R.id.radio_pirates: 79 if (checked) 80 // Pirates are the best 81 break; 82 case R.id.radio_ninjas: 83 if (checked) 84 // Ninjas rule 85 break; 86 } 87 } 88 </pre> 89 90 <p>The method you declare in the {@link android.R.attr#onClick android:onClick} attribute 91 must have a signature exactly as shown above. Specifically, the method must:</p> 92 <ul> 93 <li>Be public</li> 94 <li>Return void</li> 95 <li>Define a {@link android.view.View} as its only parameter (this will be the {@link 96 android.view.View} that was clicked)</li> 97 </ul> 98 99 <p class="note"><strong>Tip:</strong> If you need to change the radio button state 100 yourself (such as when loading a saved {@link android.preference.CheckBoxPreference}), 101 use the {@link android.widget.CompoundButton#setChecked(boolean)} or {@link 102 android.widget.CompoundButton#toggle()} method.</p> 103