My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

Layout Resource

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


In Java: R.layout.filename
In XML: @[package:]layout/filename
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup xmlns:android=""
    android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
    android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
    [ViewGroup-specific attributes] >
        android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
        android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
        [View-specific attributes] >
    <ViewGroup >
        <View />
    <include layout="@layout/layout_resource"/>
Note: The root element can be either a ViewGroup, a View, or a <merge> element, but there must be only one root element and it must contain the xmlns:android attribute with the android namespace.
A container for other View elements.Different kinds of ViewGroup objects include LinearLayout, RelativeLayout, and FrameLayout. attributes:
Resource ID. A unique resource name for the element.
Dimension or keyword. Required. The height for the group, as a dimension value (or dimension resource) or a keyword ("fill_parent" or "wrap_content").
Dimension or keyword. Required. The width for the group, as a dimension value (or dimension resource) or a keyword ("fill_parent" or "wrap_content").
An individual UI component, generally referred to as a "widget". Different kinds of View objects include TextView, Button, and CheckBox.
attributes :android:id,android:layout_height,android:layout_width.
Includes a layout file into this layout.
attributes :android:id,android:layout_height,android:layout_width.
Caution: If you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.
Another way to include a layout is to use ViewStub. It is a lightweight View that consumes no layout space until you explicitly inflate it.
Using this as the root element is useful when you know that this layout will be placed into a layout that already contains the appropriate parent View.
Value for android:id
For the ID value, you should usually use this syntax form: "@+id/name". The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the class, if it doesn't already exist. For example:
<TextView android:id="@+id/nameTextbox"/>
The nameTextbox name is now a resource ID attached to this element. You can then refer to the TextView to which the ID is associated in Java:
This code returns the TextView object.
Value for android:layout_height and android:layout_width:
Value Description
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 View and 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.
XML file saved at res/layout/main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:text="Hello, I am a Button" />
This application code will load the layout for an Activity, in the onCreate() method:
public void onCreate(Bundle savedInstanceState) {

Android - Layout