Note: The root element can be a
ViewGroup, a View, or a element, but there can be only
one root element and it must contain the xmlns:android attribute with the android
namespace as shown in the preceding syntax example.
elements:
A container for other View elements. There are many
different kinds of ViewGroup objects, and each one lets you
specify the layout of the child elements in different ways. Different kinds of
ViewGroup objects include LinearLayout,
RelativeLayout, and FrameLayout.
Don't assume that any derivation of ViewGroup
accepts nested views. Some view groups
are implementations of the AdapterView class, which determines
its children only from an Adapter.
Attributes:
android:id
Resource ID. A unique resource name for the element, which you can
use to obtain a reference to the ViewGroup from your application. For more
information, see the Value for android:id section.
The ViewGroup
base class supports more attributes, and many more are supported by each implementation of
ViewGroup. For a reference of all available attributes,
see the corresponding reference documentation for the ViewGroup class,
for example, the LinearLayout XML
attributes.
An individual UI component, generally referred to as a widget. Different
kinds of View objects include TextView,
Button, and CheckBox.
Attributes:
android:id
Resource ID. A unique resource name for the element, which you can use to
obtain a reference to the View from your application. For more
information, see the Value for android:id section.
The View
base class supports more attributes, and many more are supported by each implementation of
View. For more information, read Layouts. For
a reference of all available attributes,
see the corresponding reference documentation, for example, the TextView XML attributes.
Any element representing a View object can include this empty element,
which gives its parent initial focus on the screen. You can have only one of these
elements per file.
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_width is 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_height is also declared.
You can include any other layout attributes in the 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 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: a lightweight
view that consumes no layout space until you explicitly inflate it. When you do, it includes a
layout file defined by its android:layout attribute. For more information about using ViewStub, read Load
views on demand.
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 View to contain the children of the
element.
This is particularly useful when you plan to include this layout
in another layout file using and
this layout doesn't require a different ViewGroup container. For more
information about merging layouts, read Reuse layouts with .
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 the aapt tool creates
a new resource integer in the R.java class, if it doesn't already exist.
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:
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 the android: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 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. For more information,
see Create custom view components.
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."],[],[]]