index.jd revision cf9fe43ce95ed19e027fe34899be52816cbe7670
1page.title=User Interface
2@jd:body
3
4<div id="qv-wrapper">
5<div id="qv">
6
7  <h2>In this document</h2>
8  <ol>
9    <li><a href="#ViewHierarchy">View Hierarchy</a></li>
10    <li><a href="#Layout">Layout</a></li>
11    <li><a href="#Widgets">Widgets</a></li>
12    <li><a href="#Events">Input Events</a></li>
13    <li><a href="#Menus">Menus</a></li>
14    <li><a href="#Advanced">Advanced Topics</a>
15      <ol>
16        <li><a href="#Adapters">Adapters</a></li>
17        <li><a href="#StylesAndThemes">Styles and Themes</a></li>
18      </ol>
19    </li>
20  </ol>
21
22  <h2>Key classes</h2>
23  <ol>
24    <li>{@link android.view.View}</li>
25    <li>{@link android.view.ViewGroup}</li>
26    <li>{@link android.widget Widget classes}</li>
27  </ol>
28</div>
29</div>
30
31<p>In an Android application, the user interface is built using {@link android.view.View} and 
32{@link android.view.ViewGroup} objects. There are many types of views and view groups, each of which 
33is a descendant of the {@link android.view.View} class.</p>
34
35<p>View objects are the basic units of user interface expression on the Android platform. 
36The View class serves as the base for subclasses called "widgets," which offer fully implemented
37UI objects, like text fields and buttons. The ViewGroup class serves as the base for subclasses called "layouts,"
38which offer different kinds of layout architecture, like linear, tabular and relative.</p>
39
40<p>A View object is a data structure whose properties store the layout parameters and content for a specific 
41rectangular area of the screen. A View object handles its own measurement, layout, drawing, focus change, 
42scrolling, and key/gesture interactions for the rectangular area of the screen in which it resides. As an
43object in the user interface, a View is also a point of interaction for the user and the receiver
44of the interaction events.</p>
45
46
47<h2 id="ViewHierarchy">View Hierarchy</h2>
48
49<p>On the Android platform, you define an Activity's UI using a hierarchy of View and ViewGroup nodes, 
50as shown in the diagram below. This hierarchy tree can be as simple or complex as you need it to be, and you 
51can build it up using Android's set of predefined widgets and layouts, or with custom Views that you 
52create yourself.</p>
53
54<img src="{@docRoot}images/viewgroup.png" alt="" width="312" height="211" align="center"/>
55
56<p>
57In order to attach the view hierarchy tree to the screen for rendering, your Activity must call the
58<code>{@link android.app.Activity#setContentView(int) setContentView()}</code> 
59method and pass a reference to the root node object. The Android system
60receives this reference and uses it to invalidate, measure, and draw the tree. The root node of the hierarchy requests 
61that its child nodes draw themselves &mdash; in turn, each view group node is responsible for calling 
62upon each of its own child views to draw themselves.
63The children may request a size and location within the parent, but the parent object has the final 
64decision on where how big each child can be.  Android parses 
65the elements of your layout in-order (from the top of the hierarchy tree), instantiating the Views and 
66adding them to their parent(s). Because these are drawn in-order, if there are elements that
67overlap positions, the last one to be drawn will lie on top of others previously drawn to that space.</p>
68
69<p>For a more detailed discussion on how view hierarchies are measured
70and drawn, read <a href="how-android-draws.html">How Android Draws Views</a>.</p>
71
72
73<h2 id="Layout">Layout</h2>
74
75<p>The most common way to define your layout and express the view hierarchy is with an XML layout file.
76XML offers a human-readable structure for the layout, much like HTML. Each element in XML is 
77either a View or ViewGroup object (or descendant thereof). View objects are leaves in the tree, 
78ViewGroup objects are branches in the tree (see the View Hierarchy figure above).</p>
79<p>The name of an XML element
80is respective to the Java class that it represents. So a <code>&lt;TextView></code> element creates
81a {@link android.widget.TextView} in your UI, and a <code>&lt;LinearLayout></code> element creates
82a {@link android.widget.LinearLayout} view group. When you load a layout resource, 
83the Android system initializes these run-time objects, corresponding to the elements in your layout.</p>
84
85<p>For example, a simple vertical layout with a text view and a button looks like this:</p>
86<pre>
87&lt;?xml version="1.0" encoding="utf-8"?>
88&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
89              android:layout_width="fill_parent" 
90              android:layout_height="fill_parent"
91              android:orientation="vertical" >
92    &lt;TextView android:id="@+id/text"
93              android:layout_width="wrap_content"
94              android:layout_height="wrap_content"
95              android:text="Hello, I am a TextView" />
96    &lt;Button android:id="@+id/button"
97            android:layout_width="wrap_content"
98            android:layout_height="wrap_content"
99            android:text="Hello, I am a Button" />
100&lt;/LinearLayout>
101</pre>
102
103<p>Notice that the LinearLayout element contains both the TextView and the Button. You can nest
104another LinearLayout (or other type of view group) inside here, to lengthen the view hierarchy and create a more
105complex layout.</p>
106
107<p>For more on building a UI layout, read <a href="declaring-layout.html">XML Layouts</a>.
108
109<div class="sidebox-wrapper">
110<div class="sidebox">
111  <p><b>Tip:</b> You can also draw View and ViewGroups objects in Java code, 
112  using the <code>{@link android.view.ViewGroup#addView(View)}</code> methods 
113  to dynamically insert new View and ViewGroup objects.</p>
114</div>
115</div>
116
117<p>There are a variety of ways in which you can layout your views. Using more and different kinds of view groups,
118you can structure child views and view groups in an infinite number of ways.
119Some pre-defined view groups offered by Android (called layouts) include LinearLayout, RelativeLayout,
120TableLayout, GridLayout and others. Each offers a unique set of layout parameters that are used to define the
121positions of child views and layout structure.</p>
122<p>To learn about some of the different kinds of view groups used for a layout, 
123read <a href="layout-objects.html">Common Layout Objects</a>.</p>
124
125
126<h2 id="Widgets">Widgets</h2>
127
128<p>A widget is a View object that serves as an interface for interaction with the user. 
129Android provides a set of fully implemented 
130widgets, like buttons, checkboxes, and text-entry fields, so you can quickly build your UI. 
131Some widgets provided by Android are more complex, like a date picker, a clock, and zoom controls.
132But you're not limited to the kinds of widgets provided by the Android platform. If you'd
133like to do something more customized and create your own actionable elements, you can, by defining your own 
134View object or by extending and combining existing widgets.</p>
135<p>Read more in the <a href="custom-components.html">Custom Components</a> developer guide.</p>
136
137<p>For a list of the widgets provided by Android, see the {@link android.widget} package.</p>
138
139
140<h2 id="Events">Input Events</h2>
141
142<p>Once you've added some Views/widgets to the UI, you probably want to know about the 
143user's interaction with them, so you can perform actions. To be informed of user input events, you
144need to do one of two things:</p>
145<ul>
146  <li><strong>Define an event listener and register it with the View.</strong> More often than not,
147this is how you'll listen for events. The View class contains a collection of nested interfaces named
148On<em>&lt;something></em>Listener, each with a callback method called <code>On<em>&lt;something></em>()</code>.
149For example, {@link android.view.View.OnClickListener} (for handling "clicks" on a View),
150{@link android.view.View.OnTouchListener} (for handling touch screen events in a View), and
151{@link android.view.View.OnKeyListener} (for handling device key presses within a View). So if you want your View
152to be notified when it is "clicked" (such as when a button is selected), implement OnClickListener and define
153its <code>onClick()</code> callback method (where you perform the action upon click), and register it
154to the View with <code>{@link android.view.View#setOnClickListener(View.OnClickListener) setOnClickListener()}</code>.
155</li>
156  <li><strong>Override an existing callback method for the View.</strong> This is
157what you should do when you've implemented your own View class and want to listen for specific events 
158that occur within it. Example events you can handle include when the
159screen is touched (<code>{@link android.view.View#onTouchEvent(MotionEvent) onTouchEvent()}</code>), when
160the trackball is moved (<code>{@link android.view.View#onTrackballEvent(MotionEvent) onTrackballEvent()}</code>), 
161or when a key on the device is pressed (<code>{@link android.view.View#onKeyDown(int, KeyEvent)
162onKeyDown()}</code>). This allows you to define the default behavior for each event inside your custom View and determine
163whether the event should be passed on to some other child View. Again, these are callbacks to the View class,
164so your only chance to define them is when you 
165<a href="{@docRoot}guide/topics/ui/custom-components.html">build a custom component</a>.
166</li>
167</ul>
168
169<p>Continue reading about handling user interaction with Views in the <a
170href="ui-events.html">Input Events</a> document.</p>
171
172
173<h2 id="Menus">Menus</h2>
174
175<p>Application menus are another important part of an application's UI. Menus offers a reliable interface that reveals
176application functions and settings. The most common application menu is revealed by pressing
177the <em>Menu</em> button on the device. However, you can also add Context Menus, which may be
178revealed when the user presses
179and holds down on an item.</p>
180
181<p>Menus are also structured using a View hierarchy, but you don't define this structure yourself. Instead,
182you define the <code>{@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()}</code> or 
183<code>{@link android.app.Activity#onCreateContextMenu(ContextMenu,View,ContextMenu.ContextMenuInfo) onCreateContextMenu()}</code> 
184callback methods for your Activity and declare the items that you want to include in your menu. 
185At the appropriate time, Android will automatically create the necessary View hierarchy for the menu and 
186draw each of your menu items in it.</p>
187
188<p>Menus also handle their own events, so there's no need to register event listeners on the items in your menu.
189When an item in your menu is selected, the <code>{@link android.app.Activity#onOptionsItemSelected(MenuItem) 
190onOptionsItemSelected()}</code> or 
191<code>{@link android.app.Activity#onContextItemSelected(MenuItem) onContextItemSelected()}</code>
192method will be called by the framework.</p>
193
194<p>And just like your application layout, you have the option to declare the items for you menu in an XML file.</p>
195
196<p>Read <a href="{@docRoot}guide/topics/ui/menus.html">Menus</a> to learn more.</p>
197
198
199<h2 id="Advanced">Advanced Topics</h2>
200
201<p>Once you've grappled the fundamentals of creating a user interface, you can explore
202some advanced features for creating a more complex application interface.</p>
203
204<h3 id="Adapters">Adapters</h3>
205
206<p>Sometimes you'll want to populate a view group with some information that can't be hard-coded, instead, 
207you want to bind your view to an external source of data. To do this, you use an AdapterView as
208your view group and each child View is initialized and populated with data from the Adapter.</p>
209<p>The AdapterView object is an implementation of ViewGroup that determines its child views
210based on a given Adapter object. The Adapter acts like a courier between your data source (perhaps an
211array of external strings) and the AdapterView, which displays it. There are several implementations
212of the Adapter class, for specific tasks, such as the CursorAdapter for reading database data from a Cursor,
213or an ArrayAdapter for reading from an arbitrary array.</p>
214<p>To learn more about using an Adapter to populate your views, read 
215<a href="binding.html">Binding to Data with AdapterView</a>.</p>
216
217
218<h3 id="StylesAndThemes">Styles and Themes</h3>
219
220<p>Perhaps you're not satisfied with the look of the standard widgets. To revise them, you can create some 
221of your own styles and themes.</p>
222
223<ul>
224  <li>A style is a set of one or more formatting attributes that you can apply as a unit to individual elements 
225in your layout. For example, you could define a style that specifies a certain text size and color, then 
226apply it to only specific View elements.</li>
227  <li>A theme is a set of one or more formatting attributes that you can apply as a unit to all activities in 
228an application, or just a single activity. For example, you could define a theme that sets specific colors for 
229the window frame and the panel background, and sets text sizes and colors for menus. This theme can then be 
230applied to specific activities or the entire application.</li>
231</ul>
232
233<p>Styles and themes are resources. Android provides some default style and theme resources that you can use, 
234or you can declare your own custom style and theme resources.</p>
235<p>Learn more about using styles and themes in the
236<a href="themes.html">Styles and Themes</a> document.</p>
237