A layout resource defines the architecture for the UI in an Activity or a component of a UI.
- FILE LOCATION:
res/layout/filename.xmlThe filename will be used as the resource ID.- COMPILED RESOURCE DATATYPE:
- Resource pointer to a
View(or subclass) resource. - RESOURCE REFERENCE:
- In Java:
R.layout.filenameIn 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 either a
ViewGroup, aView, or a<merge>element, but there must be only one root element and it must contain thexmlns:androidattribute with theandroidnamespace as shown. - ELEMENTS:
-
Value for
For the ID value, you should usually use this syntax form:android:id"@+id/name". The plus symbol,+, indicates that this is a new resource ID and theaapttool will create a new resource integer in theR.javaclass, if it doesn't already exist. For example:<TextView android:id="@+id/nameTextbox"/>
ThenameTextboxname is now a resource ID attached to this element. You can then refer to theTextViewto which the ID is associated in Java:findViewById(R.id.nameTextbox);
This code returns theTextViewobject. However, if you have already defined an ID resource (and it is not already used), then you can apply that ID to aViewelement by excluding the plus symbol in theandroid:idvalue.Value for
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:android:layout_heightandandroid:layout_width:Value Description match_parentSets the dimension to match that of the parent element. Added in API Level 8 to deprecate fill_parent.wrap_contentSets the dimension only to the size required to fit the content of this element. Custom View elements
You can create your own customViewandViewGroupelements 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, see the Custom Components developer guide. - 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 will load the layout for anActivity, in theonCreate()method: -
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
- SEE ALSO:
