blob: 179c1d8e0e3e4a21bdbe52c31088ba1ba5bbaa88 [file] [log] [blame]
Dirk Dougherty22558d02009-12-10 16:25:06 -08001page.title=Layout Tricks: Creating Reusable UI Components
Scott Main796ce772011-02-16 10:04:45 -08002parent.title=Articles
3parent.link=../browser.html?tag=article
Dirk Dougherty22558d02009-12-10 16:25:06 -08004@jd:body
5
6<p>The Android platform offers a wide variety of UI <em>widgets</em>, small
7visual construction blocks that you can glue together to present users with
8complex and useful interfaces. However applications often need higher-level
9visual <em>components</em>. To meet that need, and to do so efficiently, you can
10combine multiple standard widgets into a single, reusable component. </p>
11
12<p>For example, you could create a reusable component that contains a progress
13bar and a cancel button, a panel containing two buttons (positive and negative
14actions), a panel with an icon, a title and a description, and so on. You can
15create UI components easily by writing a custom <code>View</code>, but you can
16do it even more easily using only XML.</p>
17
18<p>In Android XML layout files, each tag is mapped to an actual class instance
19(the class is always a subclass of {@link android.view.View} The UI toolkit lets
20you also use three special tags that are not mapped to a <code>View</code>
21instance: <code>&lt;requestFocus /&gt;</code>, <code>&lt;merge /&gt;</code> and
22<code>&lt;include /&gt;</code>. This article shows how to use <code>&lt;include
23/&gt;</code> to create pure XML visual components. For information about how to
24use <code>&lt;merge /&gt;</code>, which can be particularly powerful when
25combined with <code>&lt;include /&gt;</code>see the <a
26href="{@docRoot}resources/articles/layout-tricks-merge.html">Merging Layouts</a>
27article. </p>
28
29<p>The <code>&lt;include /&gt;</code> element does exactly what its name
30suggests; it includes another XML layout. Using this tag is straightforward as
31shown in the following example, taken straight from <a
32href="http://android.git.kernel.org/?p=platform/packages/apps/Launcher.git;a=
33tree;h=refs/heads/master;hb=master">the source code of the Home application</a>
34that ships with Android:</p>
35
36<pre class="prettyprint">&lt;com.android.launcher.Workspace
37 android:id="&#64;+id/workspace"
38 android:layout_width="fill_parent"
39 android:layout_height="fill_parent"
40
41 launcher:defaultScreen="1"&gt;
42
43 &lt;include android:id="&#64;+id/cell1" layout="@layout/workspace_screen" /&gt;
44 &lt;include android:id="&#64;+id/cell2" layout="@layout/workspace_screen" /&gt;
45 &lt;include android:id="&#64;+id/cell3" layout="@layout/workspace_screen" /&gt;
46
47&lt;/com.android.launcher.Workspace&gt;</pre>
48
49<p>In the <code>&lt;include /&gt;</code> only the <code>layout</code> attribute
50is required. This attribute, without the <code>android</code> namespace prefix,
51is a reference to the layout file you wish to include. In this example, the same
52layout is included three times in a row. This tag also lets you override a few
53attributes of the included layout. The above example shows that you can use
54<code>android:id</code> to specify the id of the root view of the included
55layout; it will also override the id of the included layout if one is defined.
56Similarly, you can override all the layout parameters. This means that any
57<code>android:layout_*</code> attribute can be used with the <code>&lt;include
Scott Main3577f512010-11-24 11:24:33 -080058/&gt;</code> tag. Here is an example in
59which the same layout is included twice, but only the first one overrides the layout properties:</p>
Dirk Dougherty22558d02009-12-10 16:25:06 -080060
Scott Main3577f512010-11-24 11:24:33 -080061<pre>
62&lt;!-- override the layout height and width --&gt;
63&lt;include layout="@layout/image_holder"
64 android:layout_height="fill_parent"
65 android:layout_width="fill_parent" /&gt;
66&lt;!-- do not override layout dimensions; inherit them from image_holder --&gt;
67&lt;include layout="@layout/image_holder" /&gt;
Dirk Dougherty22558d02009-12-10 16:25:06 -080068</pre>
69
Scott Main3577f512010-11-24 11:24:33 -080070<p class="caution"><strong>Caution:</strong> If you want to override the layout dimensions,
71you must override both <code>android:layout_height</code> and
72<code>android:layout_width</code>&mdash;you cannot override only the height or only the width.
73If you override only one, it will not take effect. (Other layout properties, such as weight,
74are still inherited from the source layout.)</p>
75
Dirk Dougherty22558d02009-12-10 16:25:06 -080076<p>This tag is particularly useful when you need to customize only part of your
77UI depending on the device's configuration. For instance, the main layout of
78your activity can be placed in the <code>layout/</code> directory and can
79include another layout which exists in two flavors, in <code>layout-land/</code>
80and <code>layout-port/</code>. This allows you to share most of the UI in
81portrait and landscape.</p>