Layout
How does the programmer specify where each component appears, how big each component should be, etc.?
ViewGroup superclass represents containers of views
Activity is a ViewGroupLayout classes are also ViewGroupsViewGroup, which will then
manage that widget's position/size behavior
<element attr="value" attr="value"> ... </element>
<element attr="value" attr="value" /> (self-closing)
Example: (case-sensitive)
<!-- this is a comment -->
<course name="CS 193A" quarter="16wi">
<instructor>Marty Stepp</instructor>
<ta>none</ta>
</course>
LinearLayout
<LinearLayout ...
android:orientation="horizontal"
tools:context=".MainActivity">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4
Very Long Text" />
</LinearLayout>
<LinearLayout ...
android:orientation="vertical"
tools:context=".MainActivity">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4
Very Long Text" />
</LinearLayout>
top, bottom, left, right, center|gravity on the layout to adjust all widgets;
set layout_gravity on an individual widget
<LinearLayout ...
android:orientation="vertical"
android:gravity="center|right">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4 Long Text" />
<Button ... android:text="Button 5"
android:layout_gravity="left" />
</LinearLayout>
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="B1"
android:layout_weight="1" />
<Button ... android:text="B2"
android:layout_weight="3" />
<Button ... android:text="B3"
android:layout_weight="1" />
</LinearLayout>
wrap_content: exactly large enough to fit the widget's contentmatch_parent: as wide or tall as 100% of the screen or layout64dp (not usually recommended)
<Button ...
android:layout_width="match_parent"
android:layout_height="wrap_content" />
padding to adjust all sides; paddingTop, Bottom, Left, Right for one side
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="Button 1"
android:padding="50dp" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3"
android:paddingLeft="20dp"
android:paddingBottom="40dp" />
</LinearLayout>
layout_margin to adjust all sides; layout_marginTop, Bottom, Left, Right
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="Button 1"
android:layout_margin="50dp" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp" />
</LinearLayout>
lays out widgets/views in lines of rows and columns
grid of 4 rows, 3 columns
orientation attribute defines row/column-major orderTableLayoutlayout_row and layout_column attribute
<GridLayout ...
android:rowCount="2"
android:columnCount="3"
tools:context=".MainActivity">
<Button ... android:text="Button 1" />
<Button ... android:text="Button Two" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button Four" />
<Button ... android:text="Button 5" />
<Button ... android:text="Button Six" />
</GridLayout>
<GridLayout ...
android:rowCount="2"
android:columnCount="3"
android:orientation="vertical">
<Button ... android:text="Button 1" />
<Button ... android:text="Button Two" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button Four" />
<Button ... android:text="Button 5"
android:layout_row="1"
android:layout_column="2" />
<Button ... android:text="Button Six"
android:layout_row="0"
android:layout_column="2" />
</RelativeLayout>
<GridLayout ...
android:rowCount="2"
android:columnCount="3">
<Button ... android:text="B1" />
<Button ... android:text="B2" />
<Button ... android:text="Button Number 3!" />
<Button ... android:text="B4"
android:layout_columnSpan="2"
android:layout_gravity="center" />
<Button ... android:text="B5" />
<Button ... android:text="B6"
android:layout_paddingTop="40dp"
android:layout_paddingBottom="40dp" />
<Button ... android:text="B7" />
<Button ... android:text="Button #8"
android:layout_gravity="right" />
</RelativeLayout>
Similar to GridLayout, but each widget must be put into a row
<TableLayout ...
tools:context=".MainActivity">
<TableRow>
<Button ... android:text="Button 1" />
<Button ... android:text="Button Two" />
<Button ... android:text="Button 3" />
</TableRow>
<TableRow>
<Button ... android:text="Button Four" />
<Button ... android:text="Button 5" />
<Button ... android:text="Button Six" />
</TableRow>
</GridLayout>
<OuterLayout ...>
<InnerLayout ...>
<Widget ... />
<Widget ... />
</InnerLayout>
<InnerLayout ...>
<Widget ... />
<Widget ... />
</InnerLayout>
</OuterLayout>
<LinearLayout ...
android:orientation="vertical"
android:gravity="center|top">
<Button ... android:text="B1" />
<LinearLayout ...
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|top">
<Button ... android:text="B2" />
<Button ... android:text="Button Number 3" />
<Button ... android:text="B4" />
</LinearLayout>
<Button ... android:text="B5" />
<Button ... android:text="B6"
android:layout_gravity="left" />
<LinearLayout ...
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|top">
<Button ... android:text="B7" />
<Button ... android:text="Button Number 8" />
</LinearLayout>
</LinearLayout>
intended to reduce the need for nested layouts
layout_below, above, toLeftOf, toRightOf
"@id/theID"
(the given widget must have an ID for this to work)layout_alignParentTop, Bottom, Left, Right
"true" to enable themlayout_centerHorizontal, Vertical, InParent
"true" to center the control within its parent in a dimension
<RelativeLayout ... >
<Button ... android:id="@+id/b1" android:text="B1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button ... android:id="@+id/b2" android:text="B2"
android:layout_alignParentLeft="true"
android:layout_below="@+id/b1" />
<Button ... android:id="@+id/b3" android:text="B3"
android:layout_centerHorizontal="true"
android:layout_below="@+id/b2" />
<Button ... android:id="@+id/b4" android:text="B4"
android:layout_alignParentRight="true"
android:layout_below="@+id/b2" />
<TextView ... android:id="@+id/tv1"
android:text="I'm a TextView!"
android:layout_centerInParent="true" />
<Button ... android:id="@+id/b5" android:text="B5"
android:padding="50dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp" />
</RelativeLayout>
<FrameLayout ... >
<ImageView
android:src="@drawable/jellybean"
... />
</FrameLayout>