1 page.title=Building a Simple User Interface 2 parent.title=Building Your First App 3 parent.link=index.html 4 5 trainingnavtop=true 6 previous.title=Running Your App 7 previous.link=running-app.html 8 next.title=Starting Another Activity 9 next.link=starting-activity.html 10 11 @jd:body 12 13 14 <!-- This is the training bar --> 15 <div id="tb-wrapper"> 16 <div id="tb"> 17 18 <h2>This lesson teaches you to</h2> 19 20 <ol> 21 <li><a href="#LinearLayout">Use a Linear Layout</a></li> 22 <li><a href="#TextInput">Add a Text Field</a></li> 23 <li><a href="#Strings">Add String Resources</a></li> 24 <li><a href="#Button">Add a Button</a></li> 25 <li><a href="#Weight">Make the Input Box Fill in the Screen Width</a></li> 26 </ol> 27 28 29 <h2>You should also read</h2> 30 <ul> 31 <li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a></li> 32 </ul> 33 34 35 </div> 36 </div> 37 38 39 40 <p>The graphical user interface for an Android app is built using a hierarchy of {@link 41 android.view.View} and {@link android.view.ViewGroup} objects. {@link android.view.View} objects are 42 usually UI widgets such as a button or text field and {@link android.view.ViewGroup} objects are 43 invisible view containers that define how the child views are laid out, such as in a 44 grid or a vertical list.</p> 45 46 <p>Android provides an XML vocabulary that corresponds to the subclasses of {@link 47 android.view.View} and {@link android.view.ViewGroup} so you can define your UI in XML with a 48 hierarchy of view elements.</p> 49 50 51 <div class="sidebox-wrapper"> 52 <div class="sidebox"> 53 <h2>Alternative Layouts</h2> 54 <p>Separating the UI layout into XML files is important for several reasons, 55 but it's especially important on Android because it allows you to define alternative layouts for 56 different screen sizes. For example, you can create two versions of a layout and tell 57 the system to use one on "small" screens and the other on "large" screens. For more information, 58 see the class about <a 59 href="{@docRoot}training/basics/supporting-devices/index.html">Supporting Different 60 Hardware</a>.</p> 61 </div> 62 </div> 63 64 <img src="{@docRoot}images/viewgroup.png" alt="" width="440" /> 65 <p class="img-caption"><strong>Figure 1.</strong> Illustration of how {@link 66 android.view.ViewGroup} objects form branches in the layout and contain {@link 67 android.view.View} objects.</p> 68 69 <p>In this lesson, you'll create a layout in XML that includes a text input field and a 70 button. In the following lesson, you'll respond when the button is pressed by sending the 71 content of the text field to another activity.</p> 72 73 74 75 <h2 id="LinearLayout">Use a Linear Layout</h2> 76 77 <p>Open the <code>main.xml</code> file from the <code>res/layout/</code> 78 directory (every new Android project includes this file by default).</p> 79 80 <p class="note"><strong>Note:</strong> In Eclipse, when you open a layout file, youre first shown 81 the ADT Layout Editor. This is an editor that helps you build layouts using WYSIWYG tools. For this 82 lesson, youre going to work directly with the XML, so click the <em>main.xml</em> tab at 83 the bottom of the screen to open the XML editor.</p> 84 85 <p>By default, the <code>main.xml</code> file includes a layout with a {@link 86 android.widget.LinearLayout} root view group and a {@link android.widget.TextView} child view. 87 Youre going to re-use the {@link android.widget.LinearLayout} in this lesson, but change its 88 contents and layout orientation.</p> 89 90 <p>First, delete the {@link android.widget.TextView} element and change the value 91 <a href="{@docRoot}reference/android/widget/LinearLayout.html#attr_android:orientation">{@code 92 android:orientation}</a> to be <code>"horizontal"</code>. The result looks like this:</p> 93 94 <pre> 95 <?xml version="1.0" encoding="utf-8"?> 96 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 97 android:layout_width="fill_parent" 98 android:layout_height="fill_parent" 99 android:orientation="horizontal" > 100 </LinearLayout> 101 </pre> 102 103 <p>{@link android.widget.LinearLayout} is a view group (a subclass of {@link 104 android.view.ViewGroup}) that lays out child views in either a vertical or horizontal orientation, 105 as specified by the <a 106 href="{@docRoot}reference/android/widget/LinearLayout.html#attr_android:orientation">{@code 107 android:orientation}</a> attribute. Each child of a {@link android.widget.LinearLayout} appears on 108 the screen in the order in which it appears in the XML.</p> 109 110 <p>The other two attributes, <a 111 href="{@docRoot}reference/android/view/View.html#attr_android:layout_width">{@code 112 android:layout_width}</a> and <a 113 href="{@docRoot}reference/android/view/View.html#attr_android:layout_height">{@code 114 android:layout_height}</a>, are required for all views in order to specify their size.</p> 115 116 <p>Because the {@link android.widget.LinearLayout} is the root view in the layout, it should fill 117 the entire screen area that's 118 available to the app by setting the width and height to 119 <code>"fill_parent"</code>.</p> 120 121 <p class="note"><strong>Note:</strong> Beginning with Android 2.2 (API level 8), 122 <code>"fill_parent"</code> has been renamed <code>"match_parent"</code> to better reflect the 123 behavior. The reason is that if you set a view to <code>"fill_parent"</code> it does not expand to 124 fill the remaining space after sibling views are considered, but instead expands to 125 <em>match</em> the size of the parent view no matter what—it will overlap any sibling 126 views.</p> 127 128 <p>For more information about layout properties, see the <a 129 href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layout</a> guide.</p> 130 131 132 133 <h2 id="TextInput">Add a Text Field</h2> 134 135 <p>To create a user-editable text field, add an {@link android.widget.EditText 136 <EditText>} element inside the {@link android.widget.LinearLayout <LinearLayout>}. The {@link 137 android.widget.EditText} class is a subclass of {@link android.view.View} that displays an editable 138 text field.</p> 139 140 <p>Like every {@link android.view.View} object, you must define certain XML attributes to specify 141 the {@link android.widget.EditText} object's properties. Heres how you should declare it 142 inside the {@link android.widget.LinearLayout <LinearLayout>} element:</p> 143 144 <pre> 145 <EditText android:id="@+id/edit_message" 146 android:layout_width="wrap_content" 147 android:layout_height="wrap_content" 148 android:hint="@string/edit_message" /> 149 </pre> 150 151 152 <div class="sidebox-wrapper"> 153 <div class="sidebox"> 154 <h3>About resource objects</h3> 155 <p>A resource object is simply a unique integer name that's associated with an app resource, 156 such as a bitmap, layout file, or string.</p> 157 <p>Every resource has a 158 corresponding resource object defined in your project's {@code gen/R.java} file. You can use the 159 object names in the {@code R} class to refer to your resources, such as when you need to specify a 160 string value for the <a 161 href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code android:hint}</a> 162 attribute. You can also create arbitrary resource IDs that you associate with a view using the <a 163 href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code android:id}</a> attribute, 164 which allows you to reference that view from other code.</p> 165 <p>The SDK tools generate the {@code R.java} each time you compile your app. You should never 166 modify this file by hand.</p> 167 </div> 168 </div> 169 170 <p>About these attributes:</p> 171 172 <dl> 173 <dt><a href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code android:id}</a></dt> 174 <dd>This provides a unique identifier for the view, which you can use to reference the object 175 from your app code, such as to read and manipulate the object (you'll see this in the next 176 lesson). 177 178 <p>The at-symbol (<code>@</code>) is required when you want to refer to a resource object from 179 XML, followed by the resource type ({@code id} in this case), then the resource name ({@code 180 edit_message}). (Other resources can use the same name as long as they are not the same 181 resource type—for example, the string resource uses the same name.)</p> 182 183 <p>The plus-symbol (<code>+</code>) is needed only when you're defining a resource ID for the 184 first time. It tells the SDK tools that the resource ID needs to be created. Thus, when the app is 185 compiled, the SDK tools use the ID value, <code>edit_message</code>, to create a new identifier in 186 your project's {@code gen/R.java} file that is now associated with the {@link 187 android.widget.EditText} element. Once the resource ID is created, other references to the ID do not 188 need the plus symbol. This is the only attribute that may need the plus-symbol. See the sidebox for 189 more information about resource objects.</p></dd> 190 191 <dt><a 192 href="{@docRoot}reference/android/view/View.html#attr_android:layout_width">{@code 193 android:layout_width}</a> and <a 194 href="{@docRoot}reference/android/view/View.html#attr_android:layout_height">{@code 195 android:layout_height}</a></dt> 196 <dd>Instead of using specific sizes for the width and height, the <code>"wrap_content"</code> value 197 specifies that the view should be only as big as needed to fit the contents of the view. If you 198 were to instead use <code>"fill_parent"</code>, then the {@link android.widget.EditText} 199 element would fill the screen, because it'd match the size of the parent {@link 200 android.widget.LinearLayout}. For more information, see the <a 201 href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> guide.</dd> 202 203 <dt><a 204 href="{@docRoot}reference/android/widget/TextView.html#attr_android:hint">{@code 205 android:hint}</a></dt> 206 <dd>This is a default string to display when the text field is empty. Instead of using a hard-coded 207 string as the value, the {@code "@string/edit_message"} value refers to a string resource defined 208 in a separate file. Because this value refers to an existing resource, it does not need the 209 plus-symbol. However, because you haven't defined the string resource yet, youll see a compiler 210 error when you add the {@code "@string/edit_message"} value. You'll fix this in the next section by 211 defining the string resource.</dd> 212 </dl> 213 214 215 216 <h2 id="Strings">Add String Resources</h2> 217 218 <p>When you need to add text in the user interface, you should always specify each string of text in 219 a resource file. String resources allow you to maintain a single location for all string 220 values, which makes it easier to find and update text. Externalizing the strings also allows you to 221 localize your app to different languages by providing alternative definitions for each 222 string.</p> 223 224 <p>By default, your Android project includes a string resource file at 225 <code>res/values/strings.xml</code>. Open this file, delete the existing <code>"hello"</code> 226 string, and add one for the 227 <code>"edit_message"</code> string used by the {@link android.widget.EditText <EditText>} 228 element.</p> 229 230 <p>While youre in this file, also add a string for the button youll soon add, called 231 <code>"button_send"</code>.</p> 232 233 <p>The result for <code>strings.xml</code> looks like this:</p> 234 235 <pre> 236 <?xml version="1.0" encoding="utf-8"?> 237 <resources> 238 <string name="app_name">My First App</string> 239 <string name="edit_message">Enter a message</string> 240 <string name="button_send">Send</string> 241 </resources> 242 </pre> 243 244 <p>For more information about using string resources to localize your app for several languages, 245 see the <a 246 href="{@docRoot}training/basics/supporting-devices/index.html">Supporting Various Devices</a> 247 class.</p> 248 249 250 251 252 <h2 id="Button">Add a Button</h2> 253 254 <p>Now add a {@link android.widget.Button <Button>} to the layout, immediately following the 255 {@link android.widget.EditText <EditText>} element:</p> 256 257 <pre> 258 <Button 259 android:layout_width="wrap_content" 260 android:layout_height="wrap_content" 261 android:text="@string/button_send" /> 262 </pre> 263 264 <p>The height and width are set to <code>"wrap_content"</code> so the button is only as big as 265 necessary to fit the button's text. This button doesn't need the 266 <a href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code android:id}</a> 267 attribute, because it won't be referenced from the activity code.</p> 268 269 270 271 <h2 id="Weight">Make the Input Box Fill in the Screen Width</h2> 272 273 <p>The layout is currently designed so that both the {@link android.widget.EditText} and {@link 274 android.widget.Button} widgets are only as big as necessary to fit their content, as shown in 275 figure 2.</p> 276 277 <img src="{@docRoot}images/training/firstapp/edittext_wrap.png" /> 278 <p class="img-caption"><strong>Figure 2.</strong> The {@link android.widget.EditText} and {@link 279 android.widget.Button} widgets have their widths set to 280 <code>"wrap_content"</code>.</p> 281 282 <p>This works fine for the button, but not as well for the text field, because the user might type 283 something longer and there's extra space left on the screen. So, it'd be nice to fill that width 284 using the text field. 285 {@link android.widget.LinearLayout} enables such a design with the <em>weight</em> property, which 286 you can specify using the <a 287 href="{@docRoot}reference/android/widget/LinearLayout.LayoutParams.html#weight">{@code 288 android:layout_weight}</a> attribute.</p> 289 290 <p>The weight value allows you to specify the amount of remaining space each view should consume, 291 relative to the amount consumed by sibling views, just like the ingredients in a drink recipe: "2 292 parts vodka, 1 part coffee liqueur" means two-thirds of the drink is vodka. For example, if you give 293 one view a weight of 2 and another one a weight of 1, the sum is 3, so the first view gets 2/3 of 294 the remaining space and the second view gets the rest. If you give a third view a weight of 1, 295 then the first view now gets 1/2 the remaining space, while the remaining two each get 1/4.</p> 296 297 <p>The default weight for all views is 0, so if you specify any weight value 298 greater than 0 to only one view, then that view fills whatever space remains after each view is 299 given the space it requires. So, to fill the remaining space with the {@link 300 android.widget.EditText} element, give it a weight of 1 and leave the button with no weight.</p> 301 302 <pre> 303 <EditText 304 android:layout_weight="1" 305 ... /> 306 </pre> 307 308 <p>In order to improve the layout efficiency when you specify the weight, you should change the 309 width of the {@link android.widget.EditText} to be 310 zero (0dp). Setting the width to zero improves layout performance because using 311 <code>"wrap_content"</code> as the width requires the system to calculate a width that is 312 ultimately irrelevant because the weight value requires another width calculation to fill the 313 remaining space.</p> 314 <pre> 315 <EditText 316 android:layout_weight="1" 317 android:layout_width="0dp" 318 ... /> 319 </pre> 320 321 <p>Figure 3 322 shows the result when you assign all weight to the {@link android.widget.EditText} element.</p> 323 324 <img src="{@docRoot}images/training/firstapp/edittext_gravity.png" /> 325 <p class="img-caption"><strong>Figure 3.</strong> The {@link android.widget.EditText} widget is 326 given all the layout weight, so fills the remaining space in the {@link 327 android.widget.LinearLayout}.</p> 328 329 <p>Heres how your complete layout file should now look:</p> 330 331 <pre> 332 <?xml version="1.0" encoding="utf-8"?> 333 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 334 android:layout_width="fill_parent" 335 android:layout_height="fill_parent" 336 android:orientation="horizontal"> 337 <EditText android:id="@+id/edit_message" 338 android:layout_weight="1" 339 android:layout_width="0dp" 340 android:layout_height="wrap_content" 341 android:hint="@string/edit_message" /> 342 <Button 343 android:layout_width="wrap_content" 344 android:layout_height="wrap_content" 345 android:text="@string/button_send" /> 346 </LinearLayout> 347 </pre> 348 349 <p>This layout is applied by the default {@link android.app.Activity} class 350 that the SDK tools generated when you created the project, so you can now run the app to see the 351 results:</p> 352 353 <ul> 354 <li>In Eclipse, click <strong>Run</strong> from the toolbar.</li> 355 <li>Or from a command line, change directories to the root of your Android project and 356 execute: 357 <pre> 358 ant debug 359 adb install bin/MyFirstApp-debug.apk 360 </pre></li> 361 </ul> 362 363 <p>Continue to the next lesson to learn how you can respond to button presses, read content 364 from the text field, start another activity, and more.</p> 365 366 367 368