page.title=Layout Resource parent.title=Resource Types parent.link=available-resources.html @jd:body

See also

  1. Declaring Layout

A layout resource defines the architecture for the UI in an Activity or a component of a UI.

file location:
res/layout/filename.xml
The filename will be used as the resource ID.
compiled resource datatype:
Resource pointer to a {@link android.view.View} (or subclass) resource.
resource reference:
In Java: R.layout.filename
In XML: @[package:]layout/filename
syntax:
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/name"
    android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
    android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
    [ViewGroup-specific attributes] >
    <View
        android:id="@+id/name"
        android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
        android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
        [View-specific attributes] >
        <requestFocus/>
    </View>
    <ViewGroup >
        <View />
    </ViewGroup>
</ViewGroup>

Note: The root element can be either a {@link android.view.ViewGroup} or a {@link android.view.View}, but there must be only one root element and it must contain the {@code xmlns:android} attribute with the {@code android} namespace as shown.

elements:
<ViewGroup>
A container for other {@link android.view.View} elements. There are many different kinds of {@link android.view.ViewGroup} objects and each one lets you specify the layout of the child elements in different ways. Different kinds of {@link android.view.ViewGroup} objects include {@link android.widget.LinearLayout}, {@link android.widget.RelativeLayout}, and {@link android.widget.FrameLayout}.

You should not assume that any derivation of {@link android.view.ViewGroup} will accept nested {@link android.view.View}s. Some {@link android.view.ViewGroup}s are implementations of the {@link android.widget.AdapterView} class, which determines its children only from an {@link android.widget.Adapter}.

attributes:

android:id
Resource name. A unique resource name for the element, which you can use to obtain a reference to the {@link android.view.ViewGroup} from your application. The value takes the form: "@+id/name". See more about the value for {@code android:id} below.
android:layout_height
Dimension or keyword. Required. The height for the group, as a dimension value (or dimension resource) or a keyword ({@code "fill_parent"} or {@code "wrap_content"}). See the valid values below.
android:layout_width
Dimension or keyword. Required. The width for the group, as a dimension value (or dimension resource) or a keyword ({@code "fill_parent"} or {@code "wrap_content"}). See the valid values below.

More attributes are supported by the {@link android.view.ViewGroup} base class, and many more are supported by each implementation of {@link android.view.ViewGroup}. For a reference of all available attributes, see the corresponding reference documentation for the {@link android.view.ViewGroup} class (for example, the LinearLayout XML attributes).

<View>
An individual UI component, generally referred to as a "widget". Different kinds of {@link android.view.View} objects include {@link android.widget.TextView}, {@link android.widget.Button}, and {@link android.widget.CheckBox}.

attributes:

android:id
Resource name. A unique resource name for the element, which you can use to obtain a reference to the {@link android.view.View} from your application. The value takes the form: "@+id/name". See more about the value for {@code android:id} below.
android:layout_height
Dimension or keyword. Required. The height for the element, as a dimension value (or dimension resource) or a keyword ({@code "fill_parent"} or {@code "wrap_content"}). See the valid values below.
android:layout_width
Dimension or keyword. Required. The width for the element, as a dimension value (or dimension resource) or a keyword ({@code "fill_parent"} or {@code "wrap_content"}). See the valid values below.

More attributes are supported by the {@link android.view.View} base class, and many more are supported by each implementation of {@link android.view.View}. Read Declaring Layout for more information. For a reference of all available attributes, see the corresponding reference documentation (for example, the TextView XML attributes).

<requestFocus>
Any element representing a {@link android.view.View} object can include this empty element, which gives it's parent initial focus on the screen. You can have only one of these elements per file.

Value for android:id

For the ID value, you should use this syntax form: "@+id/name". The plus symbol, {@code +}, indicates that this is a new resource ID and the aapt tool will create a new resource number to the {@code R.java} class, if it doesn't already exist. For example:

<TextView android:id="@+id/nameTextbox"/>

You can then refer to it this way in Java:

findViewById(R.id.nameTextbox);

Value for android:layout_height and android:layout_width:

The height and width value can be expressed using any of the dimension units supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:

ValueDescription
match_parent Sets the dimension to match that of the parent element. Added in API Level 8 to deprecate fill_parent.
fill_parent Sets the dimension to match that of the parent element.
wrap_content Sets the dimension only to the size required to fit the content of this element.

Custom View elements

You can create your own custom {@link android.view.View} and {@link android.view.ViewGroup} elements and apply them to your layout the same as a standard layout element. You can also specify the attributes supported in the XML element. To learn more, read Building Custom Components.

example:
XML file saved at res/layout/main_activity.xml:
<?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>

This application code will load the layout for an {@link android.app.Activity}, in the {@link android.app.Activity#onCreate(Bundle) onCreate()} method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView.(R.layout.main_activity);
}
see also: