1page.title=Hello, Form Stuff 2parent.title=Hello, Views 3parent.link=index.html 4@jd:body 5 6<p>This page introduces a variety of widgets, like image buttons, 7text fields, checkboxes and radio buttons.</p> 8 9 10<ol> 11 <li>Start a new project/Activity called HelloFormStuff.</li> 12 <li>Your layout file should have a basic LinearLayout: 13 <pre> 14<?xml version="1.0" encoding="utf-8"?> 15<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 16 android:orientation="vertical" 17 android:layout_width="fill_parent" 18 android:layout_height="fill_parent" > 19 20</LinearLayout> 21</pre> 22 <p>For each widget you want to add, just put the respective View inside here.</p> 23</li> 24</ol> 25<p class="note"><strong>Tip:</strong> As you add new Android code, press Ctrl(or Cmd) + Shift + O 26to import all needed packages.</p> 27 28 29<h2>ImageButton</h2> 30<p>A button with a custom image on it. 31We'll make it display a message when pressed.</p> 32<ol> 33 <li><img src="images/android.png" align="right"/> 34 Drag the Android image on the right (or your own image) into the 35 res/drawable/ directory of your project. 36 We'll use this for the button.</li> 37 <li>Open the layout file and, inside the LinearLayout, add the {@link android.widget.ImageButton} element: 38<pre> 39<ImageButton 40 android:id="@+id/android_button" 41 android:layout_width="100dip" 42 android:layout_height="wrap_content" 43 android:src="@drawable/android" /> 44</pre> 45 <p>The source of the button 46 is from the res/drawable/ directory, where we've placed the android.png.</p> 47 <p class="note"><strong>Tip:</strong> You can also reference some of the many built-in 48 images from the Android {@link android.R.drawable} resources, 49 like <code>ic_media_play</code>, for a "play" button image. To do so, change the source 50 attribute to <code>android:src="@android:drawable/ic_media_play"</code>.</p> 51</li> 52<li>To make the button to actually do something, add the following 53code at the end of the <code>onCreate()</code> method: 54<pre> 55final ImageButton button = (ImageButton) findViewById(R.id.android_button); 56button.setOnClickListener(new OnClickListener() { 57 public void onClick(View v) { 58 // Perform action on clicks 59 Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show(); 60 } 61}); 62</pre> 63<p>This captures our ImageButton from the layout, then adds an on-click listener to it. 64The {@link android.view.View.OnClickListener} must define the <code>onClick()</code> method, which 65defines the action to be made when the button is clicked. Here, we show a 66{@link android.widget.Toast} message when clicked.</p> 67</li> 68<li>Run it.</li> 69</ol> 70 71 72<h2>EditText</h2> 73<p>A text field for user input. We'll make it display the text entered so far when the "Enter" key is pressed.</p> 74 75<ol> 76 <li>Open the layout file and, inside the LinearLayout, add the {@link android.widget.EditText} element: 77<pre> 78<EditText 79 android:id="@+id/edittext" 80 android:layout_width="fill_parent" 81 android:layout_height="wrap_content"/> 82</pre> 83</li> 84<li>To do something with the text that the user enters, add the following code 85to the end of the <code>onCreate()</code> method: 86<pre> 87final EditText edittext = (EditText) findViewById(R.id.edittext); 88edittext.setOnKeyListener(new OnKeyListener() { 89 public boolean onKey(View v, int keyCode, KeyEvent event) { 90 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { 91 // Perform action on key press 92 Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show(); 93 return true; 94 } 95 return false; 96 } 97}); 98</pre> 99<p>This captures our EditText element from the layout, then adds an on-key listener to it. 100The {@link android.view.View.OnKeyListener} must define the <code>onKey()</code> method, which 101defines the action to be made when a key is pressed. In this case, we want to listen for the 102Enter key (when pressed down), then pop up a {@link android.widget.Toast} message with the 103text from the EditText field. Be sure to return <var>true</var> after the event is handled, 104so that the event doesn't bubble-up and get handled by the View (which would result in a 105carriage return in the text field).</p> 106<li>Run it.</li> 107</ol> 108 109 110<h2>CheckBox</h2> 111<p>A checkbox for selecting items. We'll make it display the the current state when pressed.</p> 112 113<ol> 114 <li>Open the layout file and, inside the LinearLayout, add the {@link android.widget.CheckBox} element: 115<pre> 116<CheckBox android:id="@+id/checkbox" 117 android:layout_width="wrap_content" 118 android:layout_height="wrap_content" 119 android:text="check it out" /> 120</pre> 121</li> 122<li>To do something when the state is changed, add the following code 123to the end of the <code>onCreate()</code> method: 124<pre> 125final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); 126checkbox.setOnClickListener(new OnClickListener() { 127 public void onClick(View v) { 128 // Perform action on clicks 129 if (checkbox.isChecked()) { 130 Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show(); 131 } else { 132 Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show(); 133 } 134 } 135}); 136</pre> 137<p>This captures our CheckBox element from the layout, then adds an on-click listener to it. 138The {@link android.view.View.OnClickListener} must define the <code>onClick()</code> method, which 139defines the action to be made when the checkbox is clicked. Here, we query the current state of the 140checkbox, then pop up a {@link android.widget.Toast} message that displays the current state. 141Notice that the CheckBox handles its own state change between checked and un-checked, so we just 142ask which it currently is.</p> 143<li>Run it.</li> 144</ol> 145<p class="note"><strong>Tip:</strong> If you find that you need to change the state 146in another way (such as when loading a saved {@link android.preference.CheckBoxPreference}), 147use <code>setChecked(true)</code> or <code>toggle()</code>.</p> 148 149 150<h2>RadioButton</h2> 151<p>Two mutually-exclusive radio buttons—enabling one disables the other. 152When each is pressed, we'll pop up a message.</p> 153 154<ol> 155 <li>Open the layout file and, inside the LinearLayout, add two {@link android.widget.RadioButton}s, 156inside a {@link android.widget.RadioGroup}: 157<pre> 158<RadioGroup 159 android:layout_width="fill_parent" 160 android:layout_height="wrap_content" 161 android:orientation="vertical"> 162 163 <RadioButton android:id="@+id/radio_red" 164 android:layout_width="wrap_content" 165 android:layout_height="wrap_content" 166 android:text="Red" /> 167 168 <RadioButton android:id="@+id/radio_blue" 169 android:layout_width="wrap_content" 170 android:layout_height="wrap_content" 171 android:text="Blue" /> 172 173</RadioGroup> 174</pre> 175</li> 176<li>To do something when each is selected, we'll need an OnClickListener. Unlike the other 177listeners we've created, instead of creating this one as an anonymous inner class, 178we'll create it as a new object. This way, we can re-use the OnClickLIstener for 179both RadioButtons. So, add the following code in the HelloFormStuff Activity 180(<em>outside</em> the <code>onCreate()</code> method): 181<pre> 182OnClickListener radio_listener = new OnClickListener() { 183 public void onClick(View v) { 184 // Perform action on clicks 185 RadioButton rb = (RadioButton) v; 186 Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show(); 187 } 188}; 189</pre> 190<p>Our <code>onClick()</code> method will be handed the View clicked, so the first thing to do 191is cast it into a RadioButton. Then we pop up a 192{@link android.widget.Toast} message that displays the selection.</p> 193<li>Now, at the bottom of the <code>onCreate()</code> method, add the following: 194<pre> 195 final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red); 196 final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue); 197 radio_red.setOnClickListener(radio_listener); 198 radio_blue.setOnClickListener(radio_listener); 199</pre> 200<p>This captures each of the RadioButtons from our layout and adds the newly-created 201OnClickListener to each.</p> 202<li>Run it.</li> 203</ol> 204<p class="note"><strong>Tip:</strong> If you find that you need to change the state of a 205RadioButton in another way (such as when loading a saved {@link android.preference.CheckBoxPreference}), 206use <code>setChecked(true)</code> or <code>toggle()</code>.</p> 207 208 209<h2>ToggleButton</h2> 210<p>A button used specifically for toggling something on and off.</p> 211 212<ol> 213 <li>Open the layout file and, inside the LinearLayout, add the {@link android.widget.ToggleButton} element: 214<pre> 215<ToggleButton android:id="@+id/togglebutton" 216 android:layout_width="wrap_content" 217 android:layout_height="wrap_content" /> 218</pre> 219</li> 220<li>To do something when the state is changed, add the following code 221to the end of the <code>onCreate()</code> method: 222<pre> 223final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton); 224togglebutton.setOnClickListener(new OnClickListener() { 225 public void onClick(View v) { 226 // Perform action on clicks 227 if (togglebutton.isChecked()) { 228 Toast.makeText(HelloFormStuff.this, "ON", Toast.LENGTH_SHORT).show(); 229 } else { 230 Toast.makeText(HelloFormStuff.this, "OFF", Toast.LENGTH_SHORT).show(); 231 } 232 } 233}); 234</pre> 235<p>This captures our ToggleButton element from the layout, then adds an on-click listener to it. 236The {@link android.view.View.OnClickListener} must define the <code>onClick()</code> method, which 237defines the action to be made when the button is clicked. Here, we query the current state of the 238ToggleButton, then pop up a {@link android.widget.Toast} message that displays the current state. 239Notice that the ToggleButton handles its own state change between checked and un-checked, so we just 240ask which it is.</p> 241<li>Run it.</li> 242</ol> 243 244<p class="note"><strong>Tip:</strong> By default, the text on the button is "ON" and "OFF", but 245you can change each of these with <code>setTextOn(<var>CharSequence</var>)</code> and 246<code>setTextOff(<var>CharSequence</var>)</code>. And, if you find that you need to change the state 247in another way (such as when loading a saved {@link android.preference.CheckBoxPreference}), 248use <code>setChecked(true)</code> or <code>toggle()</code>. </p> 249 250 251<p>If you've added all the form items above, your application should look something like this:</p> 252<img src="images/hello-formstuff.png" width="150px" /> 253 254<h3>References</h3> 255<ul> 256 <li>{@link android.widget.ImageButton}</li> 257 <li>{@link android.widget.EditText}</li> 258 <li>{@link android.widget.CheckBox}</li> 259 <li>{@link android.widget.RadioButton}</li> 260 <li>{@link android.widget.ToggleButton}</li> 261</ul> 262 263