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:android
attribute with theandroid
namespace as shown in the preceding syntax example. - elements:
-
Value for android:id
For 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 theaapt
tool creates a new resource integer in theR.java
class, if it doesn't already exist.<TextView android:id="@+id/nameTextbox"/>
The
nameTextbox
name is now a resource ID attached to this element. You can then refer to theTextView
to which the ID is associated in Java:Kotlin
val textView: TextView? = findViewById(R.id.nameTextbox)
Java
TextView textView = findViewById(R.id.nameTextbox);
This code returns the
TextView
object.However, if you have already defined an ID resource, and it isn't already used, then you can apply that ID to a
View
element by excluding the plus symbol in theandroid:id
value.Values for android:layout_height and android:layout_width
The 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 elements
You can create custom
View
andViewGroup
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. 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
Activity
in theonCreate()
method:Kotlin
public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) }
Java
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
- see also: