Layout resource
    
    
      
    
    
      
      Stay organized with collections
    
    
      
      Save and categorize content based on your preferences.
    
  
  
      
    
  
  
  
  
  
    
    
    
  
  
    
    
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 is used as the resource ID.
- compiled resource datatype:
- Resource pointer to a 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="@[+][package:]id/resource_name"
    android:layout_height=["dimension" | "match_parent" | "wrap_content"]
    android:layout_width=["dimension" | "match_parent" | "wrap_content"]
    [ViewGroup-specific attributes] >
    <View
        android:id="@[+][package:]id/resource_name"
        android:layout_height=["dimension" | "match_parent" | "wrap_content"]
        android:layout_width=["dimension" | "match_parent" | "wrap_content"]
        [View-specific attributes] >
        <requestFocus/>
    </View>
    <ViewGroup >
        <View />
    </ViewGroup>
    <include layout="@layout/layout_resource"/>
</ViewGroup>Note: The root element can be a
ViewGroup, aView, or a<merge>element, but there can be only
one root element and it must contain thexmlns:androidattribute with theandroidnamespace as shown in the preceding syntax example.
 
- elements:
- 
  
  - <ViewGroup>
- A container for other Viewelements. There are many
    different kinds ofViewGroupobjects, and each one lets you
    specify the layout of the child elements in different ways. Different kinds ofViewGroupobjects includeLinearLayout,RelativeLayout, andFrameLayout.Don't assume that any derivation of ViewGroupaccepts nested views. Some view groups
    are implementations of theAdapterViewclass, which determines
    its children only from anAdapter.
 Attributes: 
        - android:id
- Resource ID. A unique resource name for the element, which you can
use to obtain a reference to the ViewGroupfrom your application. For more
information, see the Value for android:id section.
- android:layout_height
- Dimension or keyword. Required. The height for the group, as a
dimension value (or
dimension resource) or a keyword
("match_parent"or"wrap_content"). For more information,
see the Values for android:layout_height and
android:layout_width section.
- android:layout_width
- Dimension or keyword. Required. The width for the group, as a
dimension value (or
dimension resource) or a keyword
("match_parent"or"wrap_content"). For more information,
see the Values for android:layout_height and
android:layout_width section.
 The ViewGroupbase class supports more attributes, and many more are supported by each implementation ofViewGroup. For a reference of all available attributes,
      see the corresponding reference documentation for theViewGroupclass,
for example, theLinearLayoutXML
attributes.
 
- <View>
- An individual UI component, generally referred to as a widget. Different
    kinds of Viewobjects includeTextView,Button, andCheckBox.Attributes: 
        - android:id
- Resource ID. A unique resource name for the element, which you can use to
          obtain a reference to the Viewfrom your application. For more
information, see the Value for android:id section.
- android:layout_height
- Dimension or keyword. Required. The height for the element, as
a dimension value (or
dimension resource) or a keyword
("match_parent"or"wrap_content").  For more information,
see the Values for android:layout_height and
android:layout_width section.
- android:layout_width
- Dimension or keyword. Required. The width for the element, as
a dimension value (or
dimension resource) or a keyword
("match_parent"or"wrap_content").  For more information,
see the Values for android:layout_height and
android:layout_width section.
 The Viewbase class supports more attributes, and many more are supported by each implementation ofView. For more information, read Layouts. For
a reference of all available attributes,
      see the corresponding reference documentation, for example, theTextViewXML attributes.
 
- <requestFocus>
- Any element representing a Viewobject can include this empty element,
    which gives its parent initial focus on the screen. You can have only one of these
    elements per file.
- <include>
- Includes a layout file into this layout.
      Attributes: 
        - layout
- Layout resource. Required. Reference to a layout
resource.
- android:id
- Resource ID. Overrides the ID given to the root view in the included layout.
        
- android:layout_height
- Dimension or keyword. Overrides the height given to the root view in the
included layout. Only effective if android:layout_widthis also declared.
- android:layout_width
- Dimension or keyword. Overrides the width given to the root view in the
included layout. Only effective if android:layout_heightis also declared.
 You can include any other layout attributes in the <include>that are
supported by the root element in the included layout and they override those defined in the
root element.
 Caution: If you want to override layout attributes using
      the <include>tag, you must override bothandroid:layout_heightandandroid:layout_widthin order for
      other layout attributes to take effect.
 Another way to include a layout is to use ViewStub: a lightweight
view that consumes no layout space until you explicitly inflate it. When you do, it includes a
layout file defined by itsandroid:layoutattribute. For more information about usingViewStub, read Load
  views on demand.
 
- <merge>
- An alternative root element that isn't drawn in the layout hierarchy. Using this as the
root element is useful when you know that this layout is placed into a layout
that already contains the appropriate parent Viewto contain the children of the<merge>element.This is particularly useful when you plan to include this layout
in another layout file using <include>and
this layout doesn't require a differentViewGroupcontainer. For more
information about merging layouts, read Reuse layouts with <include>.
 
 Value for android:idFor the ID value, you typically use this syntax form: "@+id/name", as shown in the following example. The
plus symbol,+, indicates that this is a new resource ID, and theaapttool creates
a new resource integer in theR.javaclass, if it doesn't already exist.
 <TextView android:id="@+id/nameTextbox"/> The nameTextboxname is now a resource ID attached to this element. You can then
refer to theTextViewto which the ID is associated in Java:
 Kotlinval textView: TextView? = findViewById(R.id.nameTextbox) JavaTextView textView = findViewById(R.id.nameTextbox); 
This code returns the TextViewobject.
 However, if you have already defined an ID resource, and it isn't
already used, then you can apply that ID to a Viewelement by excluding the
plus symbol in theandroid:idvalue.
 Values for android:layout_height and
android:layout_widthThe height and width values are expressed using any of the
  dimension
  units supported by Android (px, dp, sp, pt, in, mm) or with the following keywords: | Value | Description | 
|---|
 
      | match_parent | Sets the dimension to match that of the parent element. Added in API level 8 to
deprecate fill_parent. |  
      | wrap_content | Sets the dimension only to the size required to fit the content of this element. |  
 Custom view elementsYou can create custom ViewandViewGroupelements and apply them to your layout the same as a standard layout
element. You can also specify the attributes supported in the XML element. For more information,
see Create custom view 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="match_parent"
              android:layout_height="match_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 loads the layout for an Activityin theonCreate()method:
 
- 
Kotlinpublic override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
}Javapublic void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}
- see also:
- 
 
    
  
  
    
      
      
    
    
      
    
    
  
       
    
    
      
    
  
  
  Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
  Last updated 2025-02-10 UTC.
  
  
  
    
      [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[]]