150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpage.title=Layouts
233baa5ad7d8cdcc89ce4fbc3bc8cd537d5f5d639Joe Fernandezpage.tags=view,viewgroup
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project@jd:body
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<div id="qv-wrapper">
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<div id="qv">
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <h2>In this document</h2>
850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ol>
950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#write">Write the XML</a></li>
1050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#load">Load the XML Resource</a></li>
1150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#attributes">Attributes</a>
1250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <ol>
1350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <li><a href="#id">ID</a></li>
1450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <li><a href="#layout-params">Layout Parameters</a></li>
1550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    </ol>
1650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  </li>
1750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#Position">Layout Position</a></li>
1850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#SizePaddingMargins">Size, Padding and Margins</a></li>
1950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#CommonLayouts">Common Layouts</a></li>
2050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#AdapterViews">Building Layouts with an Adapter</a>
2150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <ol>
2250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <li><a href="#FillingTheLayout">Filling an adapter view with data</a></li>
2350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <li><a href="#HandlingUserSelections">Handling click events</a></li>
2450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    </ol>
2550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  </li>
2650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ol>
27bf15ce6de461ba5d2a3d823a2445155dc4e4dee9Dirk Dougherty
28ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main  <h2>Key classes</h2>
29ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main  <ol>
30ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main    <li>{@link android.view.View}</li>
31ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main    <li>{@link android.view.ViewGroup}</li>
32ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main    <li>{@link android.view.ViewGroup.LayoutParams}</li>
33ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main  </ol>
34f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
352d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Main  <h2>See also</h2>
362d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Main  <ol>
372d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Main    <li><a href="{@docRoot}training/basics/firstapp/building-ui.html">Building a Simple User
382d9063bc705aae7ef4cc90206ece03d4caeefba7Scott MainInterface</a></li> </div>
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</div>
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
412d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Main<p>A layout defines the visual structure for a user interface, such as the UI for an <a
422d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Mainhref="{@docRoot}guide/components/activities.html">activity</a> or <a
432d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Mainhref="{@docRoot}guide/topics/appwidgets/index.html">app widget</a>.
442d9063bc705aae7ef4cc90206ece03d4caeefba7Scott MainYou can declare a layout in two ways:</p>
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<ul>
46f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera<li><strong>Declare UI elements in XML</strong>. Android provides a straightforward XML
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.</li>
48f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera<li><strong>Instantiate layout elements at runtime</strong>. Your
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectapplication can create View and ViewGroup objects (and manipulate their properties) programmatically. </li>
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</ul>
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>The Android framework gives you the flexibility to use either or both of these methods for declaring and managing your application's UI. For example, you could declare your application's default layouts in XML, including the screen elements that will appear in them and their properties. You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time. </p>
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
543b3145ed6c23d99ccf6ab559552399148a5a59c9Scott Main<div class="sidebox-wrapper">
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<div class="sidebox">
56bf15ce6de461ba5d2a3d823a2445155dc4e4dee9Dirk Dougherty  <ul>
5750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>The <a href="{@docRoot}tools/sdk/eclipse-adt.html">ADT
58f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera  Plugin for Eclipse</a> offers a layout preview of your XML &mdash;
59bf15ce6de461ba5d2a3d823a2445155dc4e4dee9Dirk Dougherty  with the XML file opened, select the <strong>Layout</strong> tab.</li>
60f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera  <li>You should also try the
61f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera  <a href="{@docRoot}tools/debugging/debugging-ui.html#hierarchyViewer">Hierarchy Viewer</a> tool,
62f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera  for debugging layouts &mdash; it reveals layout property values,
63f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera  draws wireframes with padding/margin indicators, and full rendered views while
64bf15ce6de461ba5d2a3d823a2445155dc4e4dee9Dirk Dougherty  you debug on the emulator or device.</li>
6550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>The <a href="{@docRoot}tools/debugging/debugging-ui.html#layoutopt">layoutopt</a> tool lets
66bf15ce6de461ba5d2a3d823a2445155dc4e4dee9Dirk Dougherty  you quickly analyze your layouts and hierarchies for inefficiencies or other problems.</li>
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</div>
683b3145ed6c23d99ccf6ab559552399148a5a59c9Scott Main</div>
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile. For example, you can create XML layouts for different screen orientations, different device screen sizes, and different languages. Additionally, declaring the layout in XML makes it easier to visualize the structure of your UI, so it's easier to debug problems. As such, this document focuses on teaching you how to declare your layout in XML. If you're
71f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cerverainterested in instantiating View objects at runtime, refer to the {@link android.view.ViewGroup} and
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{@link android.view.View} class references.</p>
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
74d3161450fb6bd461575f0e93fadacea73cc9c51cDavid Friedman<p>In general, the XML vocabulary for declaring UI elements closely follows the structure and naming of the classes and methods, where element names correspond to class names and attribute names correspond to methods. In fact, the correspondence is often so direct that you can guess what XML attribute corresponds to a class method, or guess what class corresponds to a given XML element. However, note that not all vocabulary is identical. In some cases, there are slight naming differences. For
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectexample, the EditText element has a <code>text</code> attribute that corresponds to
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<code>EditText.setText()</code>. </p>
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p class="note"><strong>Tip:</strong> Learn more about different layout types in <a href="{@docRoot}guide/topics/ui/layout-objects.html">Common
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectLayout Objects</a>. There are also a collection of tutorials on building various layouts in the
8022558d0be8210aee1a2ab64b374bb357d4123acdDirk Dougherty<a href="{@docRoot}resources/tutorials/views/index.html">Hello Views</a> tutorial guide.</p>
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<h2 id="write">Write the XML</h2>
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML &mdash; with a series of nested elements. </p>
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>Each layout file must contain exactly one root element, which must be a View or ViewGroup object. Once you've defined the root element, you can add additional layout objects or widgets as child elements to gradually build a View hierarchy that defines your layout. For example, here's an XML layout that uses a vertical {@link android.widget.LinearLayout}
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectto hold a {@link android.widget.TextView} and a {@link android.widget.Button}:</p>
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<pre>
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project&lt;?xml version="1.0" encoding="utf-8"?>
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
91e295205ea29bbae0b1fc05970eff265f7d72651bAndrew Solovay              android:layout_width="match_parent"
92e295205ea29bbae0b1fc05970eff265f7d72651bAndrew Solovay              android:layout_height="match_parent"
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project              android:orientation="vertical" >
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    &lt;TextView android:id="@+id/text"
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project              android:layout_width="wrap_content"
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project              android:layout_height="wrap_content"
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project              android:text="Hello, I am a TextView" />
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    &lt;Button android:id="@+id/button"
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            android:layout_width="wrap_content"
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            android:layout_height="wrap_content"
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            android:text="Hello, I am a Button" />
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project&lt;/LinearLayout>
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</pre>
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
105f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera<p>After you've declared your layout in XML, save the file with the <code>.xml</code> extension,
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectin your Android project's <code>res/layout/</code> directory, so it will properly compile. </p>
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1082d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Main<p>More information about the syntax for a layout XML file is available in the <a
1092d9063bc705aae7ef4cc90206ece03d4caeefba7Scott Mainhref="{@docRoot}guide/topics/resources/layout-resource.html">Layout Resources</a> document.</p>
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<h2 id="load">Load the XML Resource</h2>
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>When you compile your application, each XML layout file is compiled into a
114f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera{@link android.view.View} resource. You should load the layout resource from your application code, in your
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{@link android.app.Activity#onCreate(android.os.Bundle) Activity.onCreate()} callback implementation.
116f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveraDo so by calling <code>{@link android.app.Activity#setContentView(int) setContentView()}</code>,
117f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cerverapassing it the reference to your layout resource in the form of:
118f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera<code>R.layout.<em>layout_file_name</em></code>.
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectFor example, if your XML layout is saved as <code>main_layout.xml</code>, you would load it
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectfor your Activity like so:</p>
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<pre>
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic void onCreate(Bundle savedInstanceState) {
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    super.onCreate(savedInstanceState);
1241318af1a476bb39b80fcadf5e545856aa2e90de2Jesse Wilson    setContentView(R.layout.main_layout);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</pre>
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>The <code>onCreate()</code> callback method in your Activity is called by the Android framework when
129f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cerverayour Activity is launched (see the discussion about lifecycles, in the
13050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<a href="{@docRoot}guide/components/activities.html#Lifecycle">Activities</a>
1319bf45a00752f84037dcf1aba79e76542b4d4ed22Scott Maindocument).</p>
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<h2 id="attributes">Attributes</h2>
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>Every View and ViewGroup object supports their own variety of XML attributes.
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectSome attributes are specific to a View object (for example, TextView supports the <code>textSize</code>
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectattribute), but these attributes are also inherited by any View objects that may extend this class.
139f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveraSome are common to all View objects, because they are inherited from the root View class (like
140f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cerverathe <code>id</code> attribute). And, other attributes are considered "layout parameters," which are
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectattributes that describe certain layout orientations of the View object, as defined by that object's
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectparent ViewGroup object.</p>
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<h3 id="id">ID</h3>
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>Any View object may have an integer ID associated with it, to uniquely identify the View within the tree.
147f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveraWhen the application is compiled, this ID is referenced as an integer, but the ID is typically
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectassigned in the layout XML file as a string, in the <code>id</code> attribute.
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectThis is an XML attribute common to all View objects
150f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera(defined by the {@link android.view.View} class) and you will use it very often.
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectThe syntax for an ID, inside an XML tag is:</p>
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<pre>android:id="&#64;+id/my_button"</pre>
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>The  at-symbol (&#64;) at the beginning of the string indicates that the XML parser should parse and expand the rest
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectof the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbe created and added to our resources (in the <code>R.java</code> file). There are a number of other ID resources that
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectare offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol,
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbut must add the <code>android</code> package namespace, like so:</p>
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<pre>android:id="&#64;android:id/empty"</pre>
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>With the <code>android</code> package namespace in place, we're now referencing an ID from the <code>android.R</code>
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectresources class, rather than the local resources class.</p>
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>In order to create views and reference them from the application, a common pattern is to:</p>
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<ol>
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <li>Define a view/widget in the layout file and assign it a unique ID:
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<pre>
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project&lt;Button android:id="&#64;+id/my_button"
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android:layout_width="wrap_content"
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android:layout_height="wrap_content"
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android:text="&#64;string/my_button_text"/>
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</pre>
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  </li>
173f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera  <li>Then create an instance of the view object and capture it from the layout
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project(typically in the <code>{@link android.app.Activity#onCreate(Bundle) onCreate()}</code> method):
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<pre>
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectButton myButton = (Button) findViewById(R.id.my_button);
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</pre>
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  </li>
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</ol>
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>Defining IDs for view objects is important when creating a {@link android.widget.RelativeLayout}.
181f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveraIn a relative layout, sibling views can define their layout relative to another sibling view,
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectwhich is referenced by the unique ID.</p>
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>An ID need not be unique throughout the entire tree, but it should be
184f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cerveraunique within the part of the tree you are searching (which may often be the entire tree, so it's best
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectto be completely unique when possible).</p>
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<h3 id="layout-params">Layout Parameters</h3>
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
190f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera<p>XML layout attributes named <code>layout_<em>something</em></code> define
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectlayout parameters for the View that are appropriate for the ViewGroup in which it resides.</p>
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>Every ViewGroup class implements a nested class that extends {@link
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid.view.ViewGroup.LayoutParams}. This subclass
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectcontains property types that define the size and position for each child view, as
19618439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainappropriate for the view group. As you can see in figure 1, the parent
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectview group defines layout parameters for each child view (including the child view group).</p>
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1992d6faf9538df94f05fe399e21de333e3a28c5122Scott Main<img src="{@docRoot}images/layoutparams.png" alt="" />
20018439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main<p class="img-caption"><strong>Figure 1.</strong> Visualization of a view hierarchy with layout
20118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Mainparameters associated with each view.</p>
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>Note that every LayoutParams subclass has its own syntax for setting
204f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cerveravalues. Each child element must define LayoutParams that are appropriate for its parent,
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectthough it may also define different LayoutParams for its own children. </p>
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2073d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty<p>All view groups include a width and height (<code>layout_width</code> and
2083d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty<code>layout_height</code>), and each view is required to define them. Many
2093d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk DoughertyLayoutParams also include optional margins and borders. <p>
2103d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty
2113d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty<p>You can specify width and height with exact measurements, though you probably
2123d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Doughertywon't want to do this often. More often, you will use one of these constants to
2133d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Doughertyset the width or height: </p>
2143d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty
2153d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty<ul>
2163d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty  <li><var>wrap_content</var> tells your view to size itself to the dimensions
217d3161450fb6bd461575f0e93fadacea73cc9c51cDavid Friedmanrequired by its content.</li>
218e295205ea29bbae0b1fc05970eff265f7d72651bAndrew Solovay  <li><var>match_parent</var> (named <var>fill_parent</var> before API Level 8)
2193d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Doughertytells your view to become as big as its parent view group will allow.</li>
2203d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty</ul>
2213d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty
2223d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty<p>In general, specifying a layout width and height using absolute units such as
2233d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Doughertypixels is not recommended. Instead, using relative measurements such as
2243d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Doughertydensity-independent pixel units (<var>dp</var>), <var>wrap_content</var>, or
225e295205ea29bbae0b1fc05970eff265f7d72651bAndrew Solovay<var>match_parent</var>, is a better approach, because it helps ensure that
2263d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Doughertyyour application will display properly across a variety of device screen sizes.
2273d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk DoughertyThe accepted measurement types are defined in the
2283d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk Dougherty<a href="{@docRoot}guide/topics/resources/available-resources.html#dimension">
2293d5f648f8fe61507107b900fc3c4cf79b19572c6Dirk DoughertyAvailable Resources</a> document.</p>
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<h2 id="Position">Layout Position</h2>
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   The geometry of a view is that of a rectangle. A view has a location,
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   two dimensions, expressed as a width and a height. The unit for location
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   and dimensions is the pixel.
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
239f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   It is possible to retrieve the location of a view by invoking the methods
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   {@link android.view.View#getLeft()} and {@link android.view.View#getTop()}. The former returns the left, or X,
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   coordinate of the rectangle representing the view. The latter returns the
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   top, or Y, coordinate of the rectangle representing the view. These methods
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   both return the location of the view relative to its parent. For instance,
246d3161450fb6bd461575f0e93fadacea73cc9c51cDavid Friedman   when <code>getLeft()</code> returns 20, that means the view is located 20 pixels to the
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   right of the left edge of its direct parent.
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
249f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   In addition, several convenience methods are offered to avoid unnecessary
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   computations, namely {@link android.view.View#getRight()} and {@link android.view.View#getBottom()}.
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   These methods return the coordinates of the right and bottom edges of the
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   rectangle representing the view. For instance, calling {@link android.view.View#getRight()}
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   is similar to the following computation: <code>getLeft() + getWidth()</code>.
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
257f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<h2 id="SizePaddingMargins">Size, Padding and Margins</h2>
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   The size of a view is expressed with a width and a height. A view actually
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   possess two pairs of width and height values.
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
264f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   The first pair is known as <em>measured width</em> and
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <em>measured height</em>. These dimensions define how big a view wants to be
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   within its parent. The
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   measured dimensions can be obtained by calling {@link android.view.View#getMeasuredWidth()}
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   and {@link android.view.View#getMeasuredHeight()}.
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
272f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   The second pair is simply known as <em>width</em> and <em>height</em>, or
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   sometimes <em>drawing width</em> and <em>drawing height</em>. These
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   dimensions define the actual size of the view on screen, at drawing time and
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   after layout. These values may, but do not have to, be different from the
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   measured width and height. The width and height can be obtained by calling
279f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera   {@link android.view.View#getWidth()} and {@link android.view.View#getHeight()}.
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
281f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   To measure its dimensions, a view takes into account its padding. The padding
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   is expressed in pixels for the left, top, right and bottom parts of the view.
285d3161450fb6bd461575f0e93fadacea73cc9c51cDavid Friedman   Padding can be used to offset the content of the view by a specific number of
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   pixels. For instance, a left padding of 2 will push the view's content by
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   2 pixels to the right of the left edge. Padding can be set using the
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   {@link android.view.View#setPadding(int, int, int, int)} method and queried by calling
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   {@link android.view.View#getPaddingLeft()}, {@link android.view.View#getPaddingTop()},
290f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera   {@link android.view.View#getPaddingRight()} and {@link android.view.View#getPaddingBottom()}.
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
292f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   <p>
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   Even though a view can define a padding, it does not provide any support for
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   margins. However, view groups provide such a support. Refer to
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   {@link android.view.ViewGroup} and
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   {@link android.view.ViewGroup.MarginLayoutParams} for further information.
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project   </p>
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
300f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera   <p>For more information about dimensions, see
301983cb86388315c4d21c0e4b52fd9db972c3c8608Joe Fernandez   <a href="{@docRoot}guide/topics/resources/more-resources.html#Dimension">Dimension Values</a>.
302983cb86388315c4d21c0e4b52fd9db972c3c8608Joe Fernandez   </p>
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
304f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
305f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
306f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera
30750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
30850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
30950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<style type="text/css">
31050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maindiv.layout {
31150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  float:left;
31250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  width:200px;
31350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  margin:0 0 20px 20px;
31450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main}
31550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maindiv.layout.first {
31650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  margin-left:0;
31750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  clear:left;
31850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main}
31950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</style>
32050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
32150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
32250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
32350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
32450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="CommonLayouts">Common Layouts</h2>
32550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
32650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Each subclass of the {@link android.view.ViewGroup} class provides a unique way to display
32750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthe views you nest within it. Below are some of the more common layout types that are built
32850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maininto the Android platform.</p>
32950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
33050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="note"><strong>Note:</strong> Although you can nest one or more layouts within another
33150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlayout to acheive your UI design, you should strive to keep your layout hierarchy as shallow as
33250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpossible. Your layout draws faster if it has fewer nested layouts (a wide view hierarchy is
33350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainbetter than a deep view hierarchy).</p>
33450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
335f246b0c4e972635a62693e6aca07911c33c10ccfRicardo Cervera<!--
33650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="framelayout">FrameLayout</h2>
33750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>{@link android.widget.FrameLayout FrameLayout} is the simplest type of layout
33850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainobject. It's basically a blank space on your screen that you can
33950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlater fill with a single object &mdash; for example, a picture that you'll swap in and out.
34050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainAll child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot
34150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainspecify a different location for a child view. Subsequent child views will simply be drawn over
34250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainprevious ones,
34350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpartially or totally obscuring them (unless the newer object is transparent).
34450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</p>
34550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main-->
34650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
34750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
34850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="layout first">
34950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <h4><a href="layout/linear.html">Linear Layout</a></h4>
35050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <a href="layout/linear.html"><img src="{@docRoot}images/ui/linearlayout-small.png" alt="" /></a>
35150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <p>A layout that organizes its children into a single horizontal or vertical row. It
35250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  creates a scrollbar if the length of the window exceeds the length of the screen.</p>
35350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
35450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
35550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="layout">
35650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <h4><a href="layout/relative.html">Relative Layout</a></h4>
35750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <a href="layout/relative.html"><img src="{@docRoot}images/ui/relativelayout-small.png" alt=""
35850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main/></a>
35950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <p>Enables you to specify the location of child objects relative to each other (child A to
36050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthe left of child B) or to the parent (aligned to the top of the parent).</p>
36150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
36250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
36350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="layout">
36450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <h4><a href="{@docRoot}guide/webapps/webview.html">Web View</a></h4>
36550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <a href="{@docRoot}guide/webapps/webview.html"><img src="{@docRoot}images/ui/webview-small.png"
36650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainalt="" /></a>
36750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <p>Displays web pages.</p>
36850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
36950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
37050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
37150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
37250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
37350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="AdapterViews" style="clear:left">Building Layouts with an Adapter</h2>
37450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
37550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>When the content for your layout is dynamic or not pre-determined, you can use a layout that
37650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainsubclasses {@link android.widget.AdapterView} to populate the layout with views at runtime. A
37750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainsubclass of the {@link android.widget.AdapterView} class uses an {@link android.widget.Adapter} to
378d3161450fb6bd461575f0e93fadacea73cc9c51cDavid Friedmanbind data to its layout. The {@link android.widget.Adapter} behaves as a middleman between the data
37950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainsource and the {@link android.widget.AdapterView} layout&mdash;the {@link android.widget.Adapter}
38013b8449515f2542867a29a47d6eaaad88680e34bScott Kennedyretrieves the data (from a source such as an array or a database query) and converts each entry
38150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maininto a view that can be added into the {@link android.widget.AdapterView} layout.</p>
38250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
38350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Common layouts backed by an adapter include:</p>
38450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
38550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="layout first">
38650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <h4><a href="layout/listview.html">List View</a></h4>
387c5e128c04808a8f1115cb8508a2c334f77eadc27Scott Main  <a href="layout/listview.html"><img src="{@docRoot}images/ui/listview-small.png" alt="" /></a>
38850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <p>Displays a scrolling single column list.</p>
38950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
39050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
39150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="layout">
39250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <h4><a href="layout/gridview.html">Grid View</a></h4>
393c5e128c04808a8f1115cb8508a2c334f77eadc27Scott Main  <a href="layout/gridview.html"><img src="{@docRoot}images/ui/gridview-small.png" alt="" /></a>
39450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <p>Displays a scrolling grid of columns and rows.</p>
39550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
39650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
39750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
39850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
39950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="FillingTheLayout" style="clear:left">Filling an adapter view with data</h3>
40050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
40150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>You can populate an {@link android.widget.AdapterView} such as {@link android.widget.ListView} or
40250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.widget.GridView} by binding the {@link android.widget.AdapterView} instance to an
40350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.widget.Adapter}, which retrieves data from an external source and creates a {@link
40450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.view.View} that represents each data entry.</p>
40550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
40650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Android provides several subclasses of {@link android.widget.Adapter} that are useful for
40750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainretrieving different kinds of data and building views for an {@link android.widget.AdapterView}. The
40850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maintwo most common adapters are:</p>
40950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
41050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<dl>
41150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <dt>{@link android.widget.ArrayAdapter}</dt>
41250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <dd>Use this adapter when your data source is an array. By default, {@link
41350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.ArrayAdapter} creates a view for each array item by calling {@link
41450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainjava.lang.Object#toString()} on each item and placing the contents in a {@link
41550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.TextView}.
41650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <p>For example, if you have an array of strings you want to display in a {@link
41750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.ListView}, initialize a new {@link android.widget.ArrayAdapter} using a
41850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainconstructor to specify the layout for each string and the string array:</p>
41950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
420f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveraArrayAdapter&lt;String> adapter = new ArrayAdapter&lt;String>(this,
42150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android.R.layout.simple_list_item_1, myStringArray);
42250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
42350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>The arguments for this constructor are:</p>
42450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ul>
42550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>Your app {@link android.content.Context}</li>
42650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>The layout that contains a {@link android.widget.TextView} for each string in the array</li>
42750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>The string array</li>
42850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ul>
42950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Then simply call
43050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.widget.ListView#setAdapter setAdapter()} on your {@link android.widget.ListView}:</p>
43150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
43250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainListView listView = (ListView) findViewById(R.id.listview);
43350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainlistView.setAdapter(adapter);
43450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
43550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
43650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <p>To customize the appearance of each item you can override the {@link
43750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainjava.lang.Object#toString()} method for the objects in your array. Or, to create a view for each
43850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainitem that's something other than a {@link android.widget.TextView} (for example, if you want an
43950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.widget.ImageView} for each array item), extend the {@link
44050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.ArrayAdapter} class and override {@link android.widget.ArrayAdapter#getView
44150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MaingetView()} to return the type of view you want for each item.</p>
44250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
44350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</dd>
44450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
44550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <dt>{@link android.widget.SimpleCursorAdapter}</dt>
44650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <dd>Use this adapter when your data comes from a {@link android.database.Cursor}. When
44750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainusing {@link android.widget.SimpleCursorAdapter}, you must specify a layout to use for each
44850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainrow in the {@link android.database.Cursor} and which columns in the {@link android.database.Cursor}
44950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainshould be inserted into which views of the layout. For example, if you want to create a list of
45050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpeople's names and phone numbers, you can perform a query that returns a {@link
45150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.database.Cursor} containing a row for each person and columns for the names and
45250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainnumbers. You then create a string array specifying which columns from the {@link
45350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.database.Cursor} you want in the layout for each result and an integer array specifying the
45450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maincorresponding views that each column should be placed:</p>
45550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
456f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveraString[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
45750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main                        ContactsContract.CommonDataKinds.Phone.NUMBER};
45850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainint[] toViews = {R.id.display_name, R.id.phone_number};
45950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
46050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>When you instantiate the {@link android.widget.SimpleCursorAdapter}, pass the layout to use for
46150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maineach result, the {@link android.database.Cursor} containing the results, and these two arrays:</p>
46250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
463f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveraSimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
46450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
46550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainListView listView = getListView();
46650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainlistView.setAdapter(adapter);
46750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
46850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>The {@link android.widget.SimpleCursorAdapter} then creates a view for each row in the
46950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.database.Cursor} using the provided layout by inserting each {@code
47050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainfromColumns} item into the corresponding {@code toViews} view.</p>.</dd>
47150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</dl>
47250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
47350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
47450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>If, during the course of your application's life, you change the underlying data that is read by
47550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainyour adapter, you should call {@link android.widget.ArrayAdapter#notifyDataSetChanged()}. This will
47650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainnotify the attached view that the data has been changed and it should refresh itself.</p>
47750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
47850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
47950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
48050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="HandlingUserSelections">Handling click events</h3>
48150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
48250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>You can respond to click events on each item in an {@link android.widget.AdapterView} by
48350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainimplementing the {@link android.widget.AdapterView.OnItemClickListener} interface. For example:</p>
48450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
48550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
48650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main// Create a message handling object as an anonymous class.
48750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainprivate OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
48850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    public void onItemClick(AdapterView parent, View v, int position, long id) {
48950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        // Do something in response to the click
49050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    }
49150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main};
49250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
493f246b0c4e972635a62693e6aca07911c33c10ccfRicardo CerveralistView.setOnItemClickListener(mMessageClickedHandler);
49450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
49550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
498