Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Building A Simple User Interface: Create A Linear Layout

This document teaches how to build a simple user interface for an Android app using XML layout files. It instructs the reader to create a linear layout with horizontal orientation to hold a text field and button. It explains that the linear layout will fill the screen and each child element will appear from top to bottom. It also describes adding an EditText element for the text field and setting attributes like ID, width and height. The goal is to allow the user to enter text and click the button to send that text to another activity.

Uploaded by

tonielhage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Building A Simple User Interface: Create A Linear Layout

This document teaches how to build a simple user interface for an Android app using XML layout files. It instructs the reader to create a linear layout with horizontal orientation to hold a text field and button. It explains that the linear layout will fill the screen and each child element will appear from top to bottom. It also describes adding an EditText element for the text field and setting attributes like ID, width and height. The goal is to allow the user to enter text and click the button to send that text to another activity.

Uploaded by

tonielhage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Building a Simple User

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

Declaring your UI layout in XML


rather than runtime code is useful for
several reasons, but it's especially
important so you can create different
layouts for different screen sizes. For
example, you can create two versions
of a layout and tell the system to use
one on "small" screens and the other
on "large" screens. For more
Figure 1. Illustration of how ViewGroup (/reference/android
information, see the class about
onnectivity & the Cloud /view/ViewGroup.html) objects form branches in the layout and
Supporting Different Devices (/training
contain other View (/reference/android/view/View.html) objects. /basics/supporting-devices/index.html).

In this lesson, you'll create a layout in XML that includes a text


field and a button. In the following lesson, you'll respond when the button is pressed by sending the
nteraction & Engagementcontent of the text field to another activity.

Create a Linear Layout


Open the fragment_main.xml file from the res/layout/ directory.

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.

First, delete the <TextView> (/reference/android/widget/TextView.html) element and change the


<RelativeLayout> (/reference/android/widget/RelativeLayout.html) element to <LinearLayout>
(/reference/android/widget/LinearLayout.html). Then add the android:orientation (/reference/android/widget
/LinearLayout.html#attr_android:orientation) attribute and set it to "horizontal". The result looks like this:

<<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>>

LinearLayout (/reference/android/widget/LinearLayout.html) is a view group (a subclass of ViewGroup


(/reference/android/view/ViewGroup.html)) that lays out child views in either a vertical or horizontal
orientation, as specified by the android:orientation (/reference/android/widget
/LinearLayout.html#attr_android:orientation) attribute. Each child of a LinearLayout (/reference/android/widget
/LinearLayout.html) appears on the screen in the order in which it appears in the XML.

The other two attributes, android:layout_width (/reference/android/view/View.html#attr_android:layout_width) and


android:layout_height (/reference/android/view/View.html#attr_android:layout_height), are required for all views in
order to specify their size.

Because the LinearLayout (/reference/android/widget/LinearLayout.html) is the root view in the layout, it


should fill the entire screen area that's available to the app by setting the width and height to
"match_parent". This value declares that the view should expand its width or height to match the width
or height of the parent view.
uilding Your First App
For more information about layout properties, see the Layout (/guide/topics/ui/declaring-layout.html) guide.
Creating an Android Project
Running Your Application
Building a Simple User Add a Text Field
To create a user-editable text field, add an <EditText> (/reference/android/widget/EditText.html) element
Starting Another Activity
inside the <LinearLayout> (/reference/android/widget/LinearLayout.html).

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" //>>

About these attributes: About resource objects

A resource object is simply a unique


android:id
integer name that's associated with
This provides a unique identifier for the view, which you an app resource, such as a bitmap,
can use to reference the object from your app code, such layout file, or string.
as to read and manipulate the object (you'll see this in the
next lesson). Every resource has a corresponding
resource object defined in your
The at sign (@) is required when you're referring to any project's gen/R.java file. You can
resource object from XML. It is followed by the resource use the object names in the R class
type (id in this case), a slash, then the resource name to refer to your resources, such as
onnectivity & the Cloud
when you need to specify a string
(edit_message).
value for the android:hint
The plus sign (+) before the resource type is needed only (/reference/android/widget
/TextView.html#attr_android:hint) attribute.
when you're defining a resource ID for the first time.
You can also create arbitrary
When you compile the app, the SDK tools use the ID
nteraction & Engagement resource IDs that you associate with
name to create a new resource ID in your project's
a view using the android:id
gen/R.java file that refers to the EditText (/reference
(/reference/android
/android/widget/EditText.html) element. Once the resource
/view/View.html#attr_android:id) attribute,
ID is declared once this way, other references to the ID do which allows you to reference that
not need the plus sign. Using the plus sign is necessary view from other code.
only when specifying a new resource ID and not needed
The SDK tools generate the R.java
for concrete resources such as strings or layouts. See the
each time you compile your app. You
sidebox for more information about resource objects.
should never modify this file by hand.
android:layout_width and android:layout_height For more information, read the guide
Instead of using specific sizes for the width and height, to Providing Resources (/guide/topics
the "wrap_content" value specifies that the view /resources/providing-resources.html).
should be only as big as needed to fit the contents of the
view. If you were to instead use "match_parent", then
the EditText element would fill the screen, because it would match the size of the parent
LinearLayout. For more information, see the Layouts guide.
android:hint
This is a default string to display when the text field is empty. Instead of using a hard-coded string
as the value, the "@string/edit_message" value refers to a string resource defined in a separate
file. Because this refers to a concrete resource (not just an identifier), it does not need the plus sign.
However, because you haven't defined the string resource yet, you’ll see a compiler error at first.
You'll fix this in the next section by defining the string.

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.

Add String Resources


When you need to add text in the user interface, you should always specify each string as a resource.
String resources allow you to manage all UI text in a single location, which makes it easier to find and
update text. Externalizing the strings also allows you to localize your app to different languages by
providing alternative definitions for each string resource.

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".

The result for strings.xml looks like this:


uilding Your First App
<?xml version="1.0" encoding="utf-8"?>
Creating an Android Project
<<rreessoouurrcceess>>
Running Your Application
<<ssttrriinngg name="app_name">>My First App<<//ssttrriinngg>>
Building a Simple User <<ssttrriinngg name="edit_message">>Enter a message<<//ssttrriinngg>>
<<ssttrriinngg name="button_send">>Send<<//ssttrriinngg>>
Starting Another Activity <<ssttrriinngg name="action_settings">>Settings<<//ssttrriinngg>>
<<ssttrriinngg name="title_activity_main">>MainActivity<<//ssttrriinngg>>
<<//rreessoouurrcceess>>

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

Make the Input Box Fill in the Screen Width


The layout is currently designed so that both the EditText (/reference/android/widget/EditText.html) and
nteraction & EngagementButton (/reference/android/widget/Button.html) widgets are only as big as necessary to fit their content, as
shown in figure 2.

Figure 2. The EditText (/reference/android/widget/EditText.html) and Button (/reference/android/widget


/Button.html) widgets have their widths set to "wrap_content".

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

Starting Another Activity


Figure 3. The EditText (/reference/android/widget/EditText.html) widget is given all the layout weight, so fills the
remaining space in the LinearLayout (/reference/android/widget/LinearLayout.html).

Here’s how your complete layout file should now look:

<?xml version="1.0" encoding="utf-8"?>


<<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">>
<<EEddiittTTeexxtt android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" //>>
<<BBuuttttoonn
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" //>>
<<//LLiinneeaarrLLaayyoouutt>>
onnectivity & the Cloud
This layout is applied by the default Activity (/reference/android/app/Activity.html) class that the SDK
tools generated when you created the project, so you can now run the app to see the results:

In Eclipse, click Run from the toolbar.


Or from a command line, change directories to the root of your Android project and execute:
nteraction & Engagement
ant debug
adb install bin/MMyyFFiirrssttAApppp-debug.apk

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.

You might also like