Android User Interfaces
Since a user will typically interact visually with an Android application, it is important to understand how Android lays out user interfaces. In this section we give a brief overview. See the Android User Interfaces document for more detail.
Visually, an Android window is a hierarchy of views, which are objects derived from the View and ViewGroup classes.
Each view in the hierarchy controls a particular rectangular subspace within the window.
The following figure illustrates a view hierarchy
The setContentView(View view) method of Activity is used to place a view hierarchy within an activity window, where the content view is the View object at the root of the view hierarchy (the topmost ViewGroup in the preceding figure). Android handles the drawing of the view hierarchy but the Activity must supply the root node as the starting point. The essential procedure is that the root node of the hierarchy requests that its child nodes draw themselves, and in turn each view group node requests that its children draw themselves. Children may request a size and location within the parent, but the parent object has the final say-so on size and position for the child objects. This will be adequate introduction for our initial purposes; a more extensive discussion of rendering the view hierarchy within an Android window may be found in How Android Draws Views.
The recommended way to lay out the view hierarchy is with an XML layout file. The name of an XML element in this file is in one-to-one correspondence with a Java class that it represents: a <TextView> element creates a TextView in your UI, a <LinearLayout> element creates a LinearLayout view group, and so on. Here is an example of a simple XML layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_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 layout would create a TextView and a Button, arrayed vertically in a linear layout in the Android window (we will explain the details of such a listing later). As long as you follow the rules, you do not have to worry about managing the relationship between XML layout of the UI and the Java code that will reference it. Basically you must just place correctly written code in a standardized set of directories and Android will do the rest. When you load a layout resource, Android automatically initializes these run-time objects, corresponding to the elements in your layout.
Menus are another important part of the UI but, as we describe below, layout and event handling are dealt with somewhat differently for menus than for other UI widgets in Android. There are two types of menus in Android:
The left figure below illustrates an options menu produced when the MENU key is pressed for an Android phone on its default screen and the right figure below shows a context menu produced on the same phone after a long press on an entry in the Contacts list.
![]() |
![]() |
| The Android Creating Menus document contains further information about creating and using both options and context menus in Android. |
In dynamical applications you will often want to populate view groups with non-static data from some external data source. For example, a display of the current temperature in several different cities must display temperature data supplied continuously from an external source. This is termed data binding to the View. In Android this is handled by using an AdapterView as the ViewGroup, with each child View populated by an Adapter. An AdapterView object is a ViewGroup that determines its child views based on a given Adapter object. The Adapter acts as a bridge between the data source and the AdapterView, which displays it.
| The binding of data to views is described more extensively in the Android documents Binding to Data with AdapterView. |
Good design is an important component of a user-friendly and aesthetically appealing user interface. One important aspect of good design is achieving a uniform look and feel for applications, which has positive implications for both aesthetics and functionality. One way to achieve this is through styles and themes:
Styles and themes allow design to be separated from content (thus they function in a way similar to Cascading Style Sheets in webpage design), which facilitates maintenance and extension of projects.
Android provides some default style and theme resources out of the box, and you can also declare your own custom styles and themes. The following figures illustrate the use of one built-in Android theme.
![]() |
![]() |
In these two screen shots from an Android phone the "About" button that can be seen in the background of the right image has been clicked to execute an intent to launch a new Activity defined by a Java class About, whose sole function is to display some "About" text. On the left the text appears unformatted on a new screen that replaces the original screen, but on the right the text appears in a rounded rectangular window floating over a dimmed view of the original screen. The only programming difference between the two images is that on the left the activity has been declared in the AndroidManifest.xml file by
<activity android:name=".About" android:label="@string/about_title"> </activity>
while on the right this declaration has been amended to add an android:theme attribute,
<activity android:name=".About" android:label="@string/about_title"
android:theme="@android:style/Theme.Dialog">
</activity>
The @android prefix on the second line indicates that what follows is defined in Android itself, rather than by the user. This added attribute implements automatically a theme Theme.Dialog that gives the look in the right image above, without the programmer having to do anything else. The Theme.Dialog causes the entire activity to be displayed in a floating window that looks like a dialog window. This example illustrates succinctly the use of styles and themes to separate content from design: the left and right figures have exactly the same content, but very different looks, and the programmer does not have to write any code to accomplish this once the theme has been defined.
Another example of a built-in Android theme can be implemented by giving the Activity the attribute android:theme="@android:style/Theme.Translucent. This has the effect of making the background of the Activity window transparent, so that the next window down in the stack is completely visible underneath the content of the new Activity window.
| A more extensive discussion of using existing styles and themes, and constructing your own, may be found in the Android Applying Styles and Themes document. |
The layout of UI widgets described above will not be very useful unless we define event handlers to respond to events associated with the widgets. We shall give various specific examples later but remark here that this can be implemented in one of two basic ways.
| A more extensive discussion of handling UI events within Views may be found in Handling UI Events. |