Building A Simple User Interface: Create A Linear Layout
Building A Simple User Interface: Create A Linear Layout
Interface
uilding Your First App
Creating an Android Project
The graphical user interface for an Android app is built using
THIS LESSON TEACHES YOU TO
a hierarchy of View (/reference/android/view/View.html) and
Running Your Application
ViewGroup (/reference/android/view/ViewGroup.html) objects. 1. Create a Linear Layout
Building a Simple User
View (/reference/android/view/View.html) objects are usually 2. Add a Text Field
3. Add String Resources
UI widgets such as buttons (/guide/topics/ui/controls/button.html) or
Starting Another Activity 4. Add a Button
text fields (/guide/topics/ui/controls/text.html) and ViewGroup 5. Make the Input Box Fill in the
(/reference/android/view/ViewGroup.html) objects are invisible Screen Width
view containers that define how the child views are laid out,
such as in a grid or a vertical list.
YOU SHOULD ALSO READ
Android provides an XML vocabulary that corresponds to the
subclasses of View (/reference/android/view/View.html) and Layouts
ViewGroup (/reference/android/view/ViewGroup.html) so you
can define your UI in XML using a hierarchy of UI elements.
Alternative Layouts
Note: In Eclipse, when you open a layout file, you’re first shown the Graphical Layout editor. This is an
editor that helps you build layouts using WYSIWYG tools. For this lesson, you’re going to work directly
with the XML, so click the fragment_main.xml tab at the bottom of the screen to open the XML editor.
The BlankActivity template you chose when you created this project includes the fragment_main.xml
file with a RelativeLayout (/reference/android/widget/RelativeLayout.html) root view and a TextView
(/reference/android/widget/TextView.html) child view.
<<LLiinneeaarrLLaayyoouutt xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >>
<<//LLiinneeaarrLLaayyoouutt>>
Like every View (/reference/android/view/View.html) object, you must define certain XML attributes to
specify the EditText (/reference/android/widget/EditText.html) object's properties. Here’s how you should
declare it inside the <LinearLayout> (/reference/android/widget/LinearLayout.html) element:
<<EEddiittTTeexxtt android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" //>>
Note: This string resource has the same name as the element ID: edit_message. However,
references to resources are always scoped by the resource type (such as id or string), so using
the same name does not cause collisions.
By default, your Android project includes a string resource file at res/values/strings.xml. Add a new
string named "edit_message" and set the value to "Enter a message." (You can delete the "hello_world"
string.)
While you’re in this file, also add a "Send" string for the button you’ll soon add, called "button_send".
For more information about using string resources to localize your app for other languages, see the
Supporting Different Devices (/training/basics/supporting-devices/index.html) class.
Add a Button
Now add a <Button> (/reference/android/widget/Button.html) to the layout, immediately following the
<EditText> (/reference/android/widget/EditText.html) element:
<<BBuuttttoonn
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" //>>
The height and width are set to "wrap_content" so the button is only as big as necessary to fit the
button's text. This button doesn't need the android:id (/reference/android/view/View.html#attr_android:id) attribute,
because it won't be referenced from the activity code.
onnectivity & the Cloud
This works fine for the button, but not as well for the text field, because the user might type something
longer. So, it would be nice to fill the unused screen width with the text field. You can do this inside a
LinearLayout (/reference/android/widget/LinearLayout.html) with the weight property, which you can
specify using the android:layout_weight (/reference/android/widget/LinearLayout.LayoutParams.html#weight)
attribute.
The weight value is a number that specifies the amount of remaining space each view should consume,
relative to the amount consumed by sibling views. This works kind of like the amount of ingredients in a
drink recipe: "2 parts vodka, 1 part coffee liqueur" means two-thirds of the drink is vodka. For example, if
you give 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 the
remaining space and the second view fills the rest. If you add a third view and give it a weight of 1, then
the first view (with weight of 2) now gets 1/2 the remaining space, while the remaining two each get 1/4.
The default weight for all views is 0, so if you specify any weight value greater than 0 to only one view,
then that view fills whatever space remains after all views are given the space they require. So, to fill the
remaining space in your layout with the EditText (/reference/android/widget/EditText.html) element, give
it a weight of 1 and leave the button with no weight.
<<EEddiittTTeexxtt
android:layout_weight="1"
... //>>
In order to improve the layout efficiency when you specify the weight, you should change the width of the
EditText (/reference/android/widget/EditText.html) to be zero (0dp). Setting the width to zero improves
layout performance because using "wrap_content" as the width requires the system to calculate a
width that is ultimately irrelevant because the weight value requires another width calculation to fill the
remaining space.
<<EEddiittTTeexxtt
android:layout_weight="1"
android:layout_width="0dp"
... //>>
uilding Your First App
Figure 3 shows the result when you assign all weight to the EditText
Creating an Android Project (/reference/android/widget
/EditText.html) element.
Running Your Application
Building a Simple User
Continue to the next lesson to learn how you can respond to button presses, read content from the text
field, start another activity, and more.