Lecture 05 UI Layouts
Lecture 05 UI Layouts
User Interface
Layouts
User Interface
• All user interface elements in an Android app are built using
View and ViewGroup objects.
– A View is an object that draws something on the screen that the user
can interact with (such as buttons and text fields).
• For Example:
– Action Bar,
– Dialogs, and
– Status Notifications.
LAYOUTS
Layouts
• A layout defines the visual structure for a user interface, such
as the UI for an activity.
– You can modify or adapt it without having to modify your source code
– You can create XML layouts for different screen orientations, different
device screen sizes, and different languages.
XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
XML Layout
• After you've declared your layout in XML, save the file with
the .xml extension, in your Android project's res/layout/
directory, so it will properly compile.
Load the XML Resource
• When you compile your application, each XML layout file is
compiled into a View resource.
R.<resource_type>.<resource_name>
Attributes
• Every View and ViewGroup object supports their own variety
of XML attributes.
• This is an XML attribute common to all View objects (defined by the View
class) and you will use it very often.
• The at‐symbol (@) at the beginning of the string indicates that the XML
parser should parse and expand the rest of the ID string and identify it as
an ID resource.
• The plus‐symbol (+) means that this is a new resource name that must be
created and added to our resources (in the R.java file).
The “id” Attribute
• When you need to reference an existing resource ID, you do not need the
plus‐symbol:
android:id="@id/my_button"
Layout Parameters
• XML layout attributes named layout_xyz define layout parameters for the
View that are appropriate for the ViewGroup in which it resides.
Layout Parameters
• All view groups include a width and height (layout_width and
layout_height), and each view is required to define them.
• More often, you will use one of these constants to set the
width or height:
Density‐independent
240 240
Pixels (“dp”)
Layout Parameters
• In general, specifying a layout width and height using absolute
units such as pixels is not recommended.
• You can specify the layout direction with the android:orientation attribute.
Linear Layout
Linear Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width="match_parent"
android:layout_height="match_parent“
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
</LinearLayout>
Linear Layout
• All children of a LinearLayout are stacked one after the other.
– A vertical list will only have one child per row, no matter how wide
they are.
– A horizontal list will only be one row high (the height of the tallest
child, plus padding).
</RelativeLayout>
Relative Layout
• A RelativeLayout is a very powerful utility for designing a user
interface because it can eliminate nested view groups and
keep your layout hierarchy flat, which improves performance.
Relative Layout
• RelativeLayout lets child views specify their position relative
to the parent view or to each other (specified by ID).
– android:layout_alignParentTop
If "true", makes the top edge of this view match the top edge of the
parent.
– android:layout_centerVertical
If "true", centers this child vertically within its parent.
– android:layout_below
Positions the top edge of this view below the view specified with a
resource ID.
– android:layout_toRightOf
Positions the left edge of this view to the right of the view specified
with a resource ID.
Layout Parameters
• The value for each layout property is either a boolean to enable a layout
position relative to the parent or an ID that references another view in the
layout against which the view should be positioned.
• In your XML layout, dependencies against other views in the layout can be
declared in any order.
• For example, you can declare that "view1" be positioned below "view2"
even if "view2" is the last view declared in the hierarchy.
Positions Relative to Parent
• android:layout_alignParentTop: Aligns view’s top with the top of the parent
• android:layout_alignParentBottom: Aligns view’s bottom with the bottom of the
parent
• android:layout_alignParentLeft: Aligns view’s left side with the left side of the
parent
• android:layout_alignParentRight: Aligns view’s right side with the right side of the
parent
• android:layout_centerHorizontal: Positions view horizontally at the center of the
parent
• android:layout_centerVertical: Positions view vertically at the center of the parent
• android:layout_centerInParent: Positions view both horizontally and vertically at
the center of the parent
Positions Relative to Other Views
• android:layout_above: Indicates that view should be placed above the view
referenced in the property
• android:layout_below: Indicates that view should be placed below the view
referenced in the property
• android:layout_toLeftOf: Indicates that view should be placed to the left of the
view referenced in the property
• android:layout_toRightOf: Indicates that view should be placed to the right of the
view referenced in the property
Positions Relative to Other Views
• android:layout_alignTop: Indicates that view’s top edge should be aligned with the
top edge of the view referenced in the property
• android:layout_alignBottom: Indicates that view’s bottom edge should be aligned
with the bottom edge of the viewreferenced in the property
• android:layout_alignLeft: Indicates that view’s left edge should be aligned with the
left edge of the view referenced in the property
• android:layout_alignRight: Indicates that view’s right edge should be aligned with
the right edge of the view referenced in the property
Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width="match_parent“
android:layout_height="match_parent“
android:paddingLeft="16dp“
android:paddingRight="16dp" >
</RelativeLayout>
Relative Layout
Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >
<EditText
android:id="@+id/name“
android:layout_width="match_parent“
android:layout_height="wrap_content“
android:hint="@string/reminder" />
</RelativeLayout>
Relative Layout
Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >
...
<Spinner
android:id="@+id/dates“
android:layout_width="0dp“
android:layout_height="wrap_content“
android:layout_below="@id/name“
android:layout_alignParentLeft="true“
android:layout_toLeftOf="@+id/times" />
</RelativeLayout>
Relative Layout
Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >
...
...
<Spinner
android:id="@id/times“
android:layout_width="96dp“
android:layout_height="wrap_content“
android:layout_below="@id/name“
android:layout_alignParentRight="true" />
</RelativeLayout>
Relative Layout
Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >
...
...
...
...
<Button
android:layout_width="96dp“
android:layout_height="wrap_content“
android:layout_below="@id/times“
android:layout_alignParentRight="true“
android:text="@string/done" />
</RelativeLayout>
Relative Layout
GRID LAYOUT
Grid Layout
• Added in API level 14, GridLayout is a layout that divides its
view space into rows, columns, and cells.
A B C
D E F
G H I
Grid Layout
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<TextView android:text=“A“
android:layout_row=1
android:layout_col=2 />
<TextView android:text=“B" />
<TextView android:text=“C" />
<TextView android:text=“D" />
<TextView android:text=“E" />
<TextView android:text=“F”
android:layout_row=0
android:layout_col=0 />
<TextView android:text=“G" />
<TextView android:text=“H" />
<TextView android:text=“I" />
</GridLayout>
Grid Layout
F B C
D E A
G H I
Grid Layout
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<TextView android:text=“A“ />
<TextView android:text=“B"
android:layout_rowSpan="2"
android:layout_gravity="fill"
android:gravity="center” />
<TextView android:text=“C" />
<TextView android:text=“D" />
<TextView android:text=“E"
android:layout_colSpan="2"
android:layout_gravity="fill“ />
<TextView android:text=“F” />
<TextView android:text=“G" />
</GridLayout>
Grid Layout
A B
C D E
F G
Grid Layout
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<TextView android:text=“A“ />
<TextView android:text=“B“ />
<TextView android:text=“C" />
<TextView android:text=“D" />
<space
android:layout_row=1
android:layout_col=1
android:layout_colSpan="2"
android:layout_gravity="fill“ />
<TextView android:text=“E" />
<TextView android:text=“F” />
<TextView android:text=“G" />
</GridLayout>
Grid Layout
A B C
E F G
References
• Android Developers: User Interface Guide
– http://developer.android.com/guide/topics/ui/index.html