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