blob: 366ddc89c9102ce6b3d0c7c55661ae9a22ed68ea [file] [log] [blame]
Scott Mainf940a1f2010-02-09 18:48:27 -08001page.title=Layout Resource
2parent.title=Resource Types
3parent.link=available-resources.html
4@jd:body
5
6<div id="qv-wrapper">
7 <div id="qv">
8 <h2>See also</h2>
9 <ol>
Scott Main50e990c2012-06-21 17:14:39 -070010 <li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Layouts</a></li>
Scott Mainf940a1f2010-02-09 18:48:27 -080011 </ol>
12 </div>
13</div>
14
15<p>A layout resource defines the architecture for the UI in an Activity or a component of a UI.</p>
16
17
18<dl class="xml">
19
20<dt>file location:</dt>
21<dd><code>res/layout/<em>filename</em>.xml</code><br/>
22The filename will be used as the resource ID.</dd>
23
24<dt>compiled resource datatype:</dt>
25<dd>Resource pointer to a {@link android.view.View} (or subclass) resource.</dd>
26
27<dt>resource reference:</dt>
28<dd>
29In Java: <code>R.layout.<em>filename</em></code><br/>
30In XML: <code>@[<em>package</em>:]layout/<em>filename</em></code>
31</dd>
32
33<dt>syntax:</dt>
34<dd>
35<pre class="stx">
36&lt;?xml version="1.0" encoding="utf-8"?>
37&lt;<a href="#viewgroup-element"><em>ViewGroup</em></a> xmlns:android="http://schemas.android.com/apk/res/android"
Scott Maindfe5c202010-06-08 15:54:52 -070038 android:id="@[+][<em>package</em>:]id/<em>resource_name</em>"
Scott Mainc6cb8a72010-04-09 15:52:18 -070039 android:layout_height=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
40 android:layout_width=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
41 [<em>ViewGroup-specific attributes</em>] &gt;
Scott Mainf940a1f2010-02-09 18:48:27 -080042 &lt;<a href="#view-element"><em>View</em></a>
Scott Maindfe5c202010-06-08 15:54:52 -070043 android:id="@[+][<em>package</em>:]id/<em>resource_name</em>"
Scott Mainc6cb8a72010-04-09 15:52:18 -070044 android:layout_height=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
45 android:layout_width=["<em>dimension</em>" | "fill_parent" | "wrap_content"]
46 [<em>View-specific attributes</em>] &gt;
Scott Mainf940a1f2010-02-09 18:48:27 -080047 &lt;<a href="#requestfocus-element">requestFocus</a>/&gt;
48 &lt;/<em>View</em>&gt;
49 &lt;<a href="#viewgroup-element"><em>ViewGroup</em></a> &gt;
50 &lt;<a href="#view-element"><em>View</em></a> /&gt;
51 &lt;/<em>ViewGroup</em>&gt;
Scott Maindfe5c202010-06-08 15:54:52 -070052 &lt;<a href="#include-element">include</a> layout="@layout/<i>layout_resource</i>"/&gt;
Scott Mainf940a1f2010-02-09 18:48:27 -080053&lt;/<em>ViewGroup</em>&gt;
54</pre>
55<p class="note"><strong>Note:</strong> The root element can be either a
Scott Maindfe5c202010-06-08 15:54:52 -070056{@link android.view.ViewGroup}, a {@link android.view.View}, or a <a
57href="#merge-element">{@code &lt;merge&gt;}</a> element, but there must be only
Scott Mainf940a1f2010-02-09 18:48:27 -080058one root element and it must contain the {@code xmlns:android} attribute with the {@code android}
59namespace as shown.</p>
60</dd>
61
62<dt>elements:</dt>
63<dd>
64 <dl class="tag-list">
65
66 <dt id="viewgroup-element"><code>&lt;ViewGroup&gt;</code></dt>
67 <dd>A container for other {@link android.view.View} elements. There are many
68 different kinds of {@link android.view.ViewGroup} objects and each one lets you
69 specify the layout of the child elements in different ways. Different kinds of
70 {@link android.view.ViewGroup} objects include {@link android.widget.LinearLayout},
71 {@link android.widget.RelativeLayout}, and {@link android.widget.FrameLayout}.
72 <p>You should not assume that any derivation of {@link android.view.ViewGroup}
73 will accept nested {@link android.view.View}s. Some {@link android.view.ViewGroup}s
74 are implementations of the {@link android.widget.AdapterView} class, which determines
75 its children only from an {@link android.widget.Adapter}.</p>
76 <p class="caps">attributes:</p>
77 <dl class="atn-list">
78 <dt><code>android:id</code></dt>
Scott Maindfe5c202010-06-08 15:54:52 -070079 <dd><em>Resource ID</em>. A unique resource name for the element, which you can
80use to obtain a reference to the {@link android.view.ViewGroup} from your application. See more
81about the <a href="#idvalue">value for {@code android:id}</a> below.
Scott Mainf940a1f2010-02-09 18:48:27 -080082 </dd>
83 <dt><code>android:layout_height</code></dt>
84 <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The height for the group, as a
85dimension value (or <a
86href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
87or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
88 </dd>
89 <dt><code>android:layout_width</code></dt>
90 <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The width for the group, as a
91dimension value (or <a
92href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
93or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
94 </dd>
95 </dl>
96 <p>More attributes are supported by the {@link android.view.ViewGroup}
97 base class, and many more are supported by each implementation of
98 {@link android.view.ViewGroup}. For a reference of all available attributes,
99 see the corresponding reference documentation for the {@link android.view.ViewGroup} class
100(for example, the <a
Scott Main5dd0fd52010-05-12 14:49:38 -0700101 href="{@docRoot}reference/android/widget/LinearLayout.html#lattrs">LinearLayout XML
102attributes</a>).</p>
Scott Mainf940a1f2010-02-09 18:48:27 -0800103 </dd>
104 <dt id="view-element"><code>&lt;View&gt;</code></dt>
105 <dd>An individual UI component, generally referred to as a "widget". Different
106 kinds of {@link android.view.View} objects include {@link android.widget.TextView},
107 {@link android.widget.Button}, and {@link android.widget.CheckBox}.
108 <p class="caps">attributes:</p>
109 <dl class="atn-list">
110 <dt><code>android:id</code></dt>
Scott Maindfe5c202010-06-08 15:54:52 -0700111 <dd><em>Resource ID</em>. A unique resource name for the element, which you can use to
112 obtain a reference to the {@link android.view.View} from your application. See more about
113the <a href="#idvalue">value for {@code android:id}</a> below.
Scott Mainf940a1f2010-02-09 18:48:27 -0800114 </dd>
115 <dt><code>android:layout_height</code></dt>
116 <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The height for the element, as
117a dimension value (or <a
118href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
119or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
120 </dd>
121 <dt><code>android:layout_width</code></dt>
122 <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The width for the element, as
123a dimension value (or <a
124href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"}
125or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below.
126 </dd>
127 </dl>
128 <p>More attributes are supported by the {@link android.view.View}
129 base class, and many more are supported by each implementation of
Scott Mainb10b48f2011-09-13 16:40:52 -0700130 {@link android.view.View}. Read <a
Scott Main50e990c2012-06-21 17:14:39 -0700131href="{@docRoot}guide/topics/ui/declaring-layout.html">Layouts</a> for more information. For
Scott Mainb10b48f2011-09-13 16:40:52 -0700132a reference of all available attributes,
Scott Mainf940a1f2010-02-09 18:48:27 -0800133 see the corresponding reference documentation (for example, the <a
134 href="{@docRoot}reference/android/widget/TextView.html#lattrs">TextView XML attributes</a>).</p>
135 </dd>
136 <dt id="requestfocus-element"><code>&lt;requestFocus&gt;</code></dt>
137 <dd>Any element representing a {@link android.view.View} object can include this empty element,
kmccormickb9dceb82013-04-03 15:11:15 -0700138 which gives its parent initial focus on the screen. You can have only one of these
Scott Mainf940a1f2010-02-09 18:48:27 -0800139 elements per file.</dd>
140
Scott Maindfe5c202010-06-08 15:54:52 -0700141 <dt id="include-element"><code>&lt;include&gt;</code></dt>
142 <dd>Includes a layout file into this layout.
143 <p class="caps">attributes:</p>
144 <dl class="atn-list">
145 <dt><code>layout</code></dt>
146 <dd><em>Layout resource</em>. <strong>Required</strong>. Reference to a layout
147resource.</dd>
148 <dt><code>android:id</code></dt>
149 <dd><em>Resource ID</em>. Overrides the ID given to the root view in the included layout.
150 </dd>
151 <dt><code>android:layout_height</code></dt>
152 <dd><em>Dimension or keyword</em>. Overrides the height given to the root view in the
Scott Main3577f512010-11-24 11:24:33 -0800153included layout. Only effective if <code>android:layout_width</code> is also declared.
Scott Maindfe5c202010-06-08 15:54:52 -0700154 </dd>
155 <dt><code>android:layout_width</code></dt>
156 <dd><em>Dimension or keyword</em>. Overrides the width given to the root view in the
Scott Main3577f512010-11-24 11:24:33 -0800157included layout. Only effective if <code>android:layout_height</code> is also declared.
Scott Maindfe5c202010-06-08 15:54:52 -0700158 </dd>
159 </dl>
160 <p>You can include any other layout attributes in the <code>&lt;include&gt;</code> that are
161supported by the root element in the included layout and they will override those defined in the
162root element.</p>
163
Scott Main9f061e22012-07-23 18:32:03 -0700164 <p class="caution"><strong>Caution:</strong> If you want to override layout attributes using
165 the <code>&lt;include&gt;</code> tag, you must override both
166 <code>android:layout_height</code> and <code>android:layout_width</code> in order for
167 other layout attributes to take effect.</p>
Scott Main3577f512010-11-24 11:24:33 -0800168
Scott Maindfe5c202010-06-08 15:54:52 -0700169 <p>Another way to include a layout is to use {@link android.view.ViewStub}. It is a lightweight
170View that consumes no layout space until you explicitly inflate it, at which point, it includes a
171layout file defined by its {@code android:layout} attribute. For more information about using {@link
Scott Mainf284d492012-07-31 09:46:52 -0700172android.view.ViewStub}, read <a href="{@docRoot}training/improving-layouts/loading-ondemand.html">Loading
173 Views On Demand</a>.</p>
Scott Maindfe5c202010-06-08 15:54:52 -0700174 </dd>
175
176 <dt id="merge-element"><code>&lt;merge&gt;</code></dt>
177 <dd>An alternative root element that is not drawn in the layout hierarchy. Using this as the
178root element is useful when you know that this layout will be placed into a layout
179that already contains the appropriate parent View to contain the children of the
180<code>&lt;merge&gt;</code> element. This is particularly useful when you plan to include this layout
181in another layout file using <a href="#include-element"><code>&lt;include&gt;</code></a> and
182this layout doesn't require a different {@link android.view.ViewGroup} container. For more
183information about merging layouts, read <a
Scott Mainf284d492012-07-31 09:46:52 -0700184href="{@docRoot}training/improving-layouts/reusing-layouts.html">Re-using Layouts with &lt;include/></a>.</dd>
Scott Maindfe5c202010-06-08 15:54:52 -0700185
Scott Mainf940a1f2010-02-09 18:48:27 -0800186 </dl>
187
Scott Maindfe5c202010-06-08 15:54:52 -0700188
189
Scott Mainf940a1f2010-02-09 18:48:27 -0800190<h4 id="idvalue">Value for <code>android:id</code></h4>
191
Scott Maindfe5c202010-06-08 15:54:52 -0700192<p>For the ID value, you should usually use this syntax form: <code>"@+id/<em>name</em>"</code>. The
193plus symbol, {@code +}, indicates that this is a new resource ID and the <code>aapt</code> tool will
194create a new resource integer in the {@code R.java} class, if it doesn't already exist. For
195example:</p>
Scott Mainf940a1f2010-02-09 18:48:27 -0800196<pre>
197&lt;TextView android:id="@+id/nameTextbox"/>
198</pre>
Scott Maindfe5c202010-06-08 15:54:52 -0700199<p>The <code>nameTextbox</code> name is now a resource ID attached to this element. You can then
200refer to the {@link android.widget.TextView} to which the ID is associated in Java:</p>
Scott Mainf940a1f2010-02-09 18:48:27 -0800201<pre>
202findViewById(R.id.nameTextbox);
203</pre>
Scott Maindfe5c202010-06-08 15:54:52 -0700204<p>This code returns the {@link android.widget.TextView} object.</p>
205
206<p>However, if you have already defined an <a
207href="{@docRoot}guide/topics/resources/drawable-resource.html#Id">ID resource</a> (and it is not
208already used), then you can apply that ID to a {@link android.view.View} element by excluding the
209plus symbol in the <code>android:id</code> value.</p>
Scott Mainf940a1f2010-02-09 18:48:27 -0800210
211<h4 id="layoutvalues">Value for <code>android:layout_height</code> and
212<code>android:layout_width</code>:</h4>
213
214 <p>The height and width value can be expressed using any of the
215 <a href="more-resources.html#Dimension">dimension
216 units</a> supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:</p>
217 <table><tr><th>Value</th><th>Description</th></tr>
218 <tr>
219 <td><code>match_parent</code></td>
220 <td>Sets the dimension to match that of the parent element. Added in API Level 8 to
221deprecate <code>fill_parent</code>.</td>
222 </tr>
223 <tr>
224 <td><code>fill_parent</code></td>
225 <td>Sets the dimension to match that of the parent element.</td>
226 </tr><tr>
227 <td><code>wrap_content</code></td>
228 <td>Sets the dimension only to the size required to fit the content of this element.</td>
229 </tr>
230 </table>
231
232<h4>Custom View elements</h4>
233
234<p>You can create your own custom {@link android.view.View} and {@link android.view.ViewGroup}
235elements and apply them to your layout the same as a standard layout
236element. You can also specify the attributes supported in the XML element. To learn more,
Scott Mainb10b48f2011-09-13 16:40:52 -0700237see the <a href="{@docRoot}guide/topics/ui/custom-components.html">Custom Components</a> developer
238guide.
Scott Mainf940a1f2010-02-09 18:48:27 -0800239</p>
240
241</dd> <!-- end elements and attributes -->
242
243<dt>example:</dt>
244<dd>XML file saved at <code>res/layout/main_activity.xml</code>:
245<pre>
246&lt;?xml version="1.0" encoding="utf-8"?>
247&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
248 android:layout_width="fill_parent"
249 android:layout_height="fill_parent"
250 android:orientation="vertical" >
251 &lt;TextView android:id="@+id/text"
252 android:layout_width="wrap_content"
253 android:layout_height="wrap_content"
254 android:text="Hello, I am a TextView" />
255 &lt;Button android:id="@+id/button"
256 android:layout_width="wrap_content"
257 android:layout_height="wrap_content"
258 android:text="Hello, I am a Button" />
259&lt;/LinearLayout>
260</pre>
261 <p>This application code will load the layout for an {@link android.app.Activity}, in the
262 {@link android.app.Activity#onCreate(Bundle) onCreate()} method:</dt>
263 <dd>
264<pre>
265public void onCreate(Bundle savedInstanceState) {
266 super.onCreate(savedInstanceState);
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800267 setContentView(R.layout.main_activity);
Scott Mainf940a1f2010-02-09 18:48:27 -0800268}
269</pre>
270</dd> <!-- end example -->
271
272
273<dt>see also:</dt>
274<dd>
275<ul>
Scott Main50e990c2012-06-21 17:14:39 -0700276 <li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Layouts</a></li>
Scott Mainf940a1f2010-02-09 18:48:27 -0800277 <li>{@link android.view.View}</li>
278 <li>{@link android.view.ViewGroup}</li>
279</ul>
280</dd>
281
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800282</dl>