Android Chapter04 User Interfaces
Android Chapter04 User Interfaces
• View is the base class for widgets, which are used to create
interactive UI components (buttons, text fields, etc.).
• The ViewGroup subclass is the base class for layouts, which are
invisible containers that hold other Views (or other ViewGroups) and
define their layout properties.
2
4. Android – UI - User Interfaces
Using Views
All of the views in a window are arranged in a single tree.
You can add views either from code or by specifying a tree of views in
one or more XML layout files.
Once you have created a tree of views, there are typically a few types of
common operations you may wish to perform:
3
4. Android – UI - User Interfaces
4
4. Android – UI - User Interfaces
GalleryView
TabWidget
Spinner
MapView
AutoCompleteTextView ListView
It is a version of the EditText A ListView is a View that
widget that will provide shows items in a vertically
auto-complete suggestions scrolling list. The items are
as the user types. The acquired from a ListAdapter.
suggestions are extracted
from a collection of strings.
6
4. Android – UI - User Interfaces
7
4. Android – UI - User Interfaces
You could create Layout XML files using UI tools such as:
8
4. Android – UI - User Interfaces
Example:
If a Button element has an attribute value of
android:textStyle = "bold"
that means that the text appearing on the face of the button
should be rendered in a boldface font style.
9
4. Android – UI - User Interfaces
An example
The application places a button to occupy the screen.
When clicked the button’s text shows current time.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onCreate(Bundle icicle) {
[Link](icicle);
setContentView([Link]);
btn = (Button) findViewById([Link]);
[Link](new OnClickListener() {
@Override
public void onClick(View v) {
updateTime();
}
});
}// onCreate
//
private void updateTime() {
[Link](new Date().toString());
}
} 10
4. Android – UI - User Interfaces
An example
This is the XML-Layout definition
Because we want to reference this button from our Java code, we need to give
it an identifier via the android:id attribute.
11
4. Android – UI - User Interfaces
An example cont.
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="[Link]
android:id="@+id/myButton"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
The remaining attributes are properties of this Button instance:
• android:text indicates the initial text to be displayed on the button face (in this case,
an empty string)
12
4. Android – UI - User Interfaces
UI
Tree
Android Layouts
Each element in the XML Layout is either a View or ViewGroup object
14
4. Android – UI - User Interfaces
Android Layouts
Displaying the Application’s View
The Android UI Framework paints the screen by walking the View tree by
asking each component to draw itself in a pre-order traversal way.
Each component draws itself and then asks each of its children to do the
same.
See: Android – Application Development, by R. Rogers et al. O’Reilly Pub. 2009, ISBN 978-0-596-52147-0
15
4. Android – UI - User Interfaces
Android Layouts
Example: Display UI Hierarchy
Using SDK
older than r8.
vertical
Horizontal 1
Horizontal 2
See: Android – Application Development, by R. Rogers et al. O’Reilly Pub. 2009, ISBN 978-0-596-52147-0
16
4. Android – UI - User Interfaces
Android Layouts
Example: Display UI Hierarchy (Using SDK Revision 8)
vertical
UI
Tree
Horizontal 1
Horizontal 2
17
4. Android – UI - User Interfaces
Android Layouts
Example: Display UI Hierarchy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android="[Link]
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/txtXcoord" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="X Coord"
android:layout_weight="1">
</EditText>
<EditText android:id="@+id/edtYcoord" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Y Coord"
android:layout_weight="1">
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btnRed" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="red"
android:layout_weight="1">
</Button>
<Button android:id="@+id/btnGreen" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="green"
android:layout_weight="1">
</Button>
</LinearLayout>
</LinearLayout>
See: Android – Application Development, by R. Rogers et al. O’Reilly Pub. 2009, ISBN 978-0-596-52147-0
18
4. Android – UI - User Interfaces
Common Layouts
There are five basic types of Layouts:
Frame, Linear, Relative, Table, and Absolute.
1. FrameLayout
FrameLayout is the simplest type of layout object. It's basically a blank
space on your screen that you can later fill with a single object — for
example, a picture that you'll swap in and out.
All child elements of the FrameLayout are pinned to the top left corner of
the screen; you cannot specify a different location for a child view.
Subsequent child views will simply be drawn over previous ones, partially
or totally obscuring them (unless the newer object is transparent).
19
4. Android – UI - User Interfaces
Common Layouts
2. LinearLayout
LinearLayout aligns all children in a single direction — vertically or
horizontally depending on the android:orientation attribute.
20
4. Android – UI - User Interfaces
Common Layouts
2. LinearLayout
You may attribute a weight to children of a LinearLayout.
Weight gives an "importance" value to a view, and allows it to expand to fill any remaining
space in the parent view.
Example:
The following two forms represent a LinearLayout
with a set of elements: a button, some labels and
text boxes. The text boxes have their width set to
fill_parent; other elements are set to wrap_content.
The gravity, by default, is left.
21
4. Android – UI - User Interfaces
Common Layouts
3. TableLayout
1. TableLayout positions its children into rows and columns.
2. TableLayout containers do not display border lines.
3. The table will have as many columns as the row with the most cells.
4. A cell could be empty, but cannot span columns, as they can in HTML.
5. A TableRow object defines a single row in the table.
6. A row has zero or more cells, each cell is defined by any kind of other View.
7. A cell may also be a ViewGroup object.
22
4. Android – UI - User Interfaces
Common Layouts
<?xml version="1.0" encoding="utf-8"?> TableLayout Example
<TableLayout
xmlns:android="[Link]
The following sample layout has
android:layout_width="fill_parent" two rows and two cells in each.
android:layout_height="fill_parent" The accompanying screenshot
android:stretchColumns="*"> shows the result, with cell
<TableRow> borders displayed as dotted
<TextView android:text="Open…" lines (added for visual effect).
android:padding="3dip" />
<TextView android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="Save As…"
android:padding="3dip" />
<TextView android:text="Ctrl-Shift-S"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout> 23
4. Android – UI - User Interfaces
Common Layouts
4. RelativeLayout
1. RelativeLayout lets child views specify their position relative to the parent
view or to each other (specified by ID).
2. You can align two elements by right border, or make one below another,
centered in the screen, centered left, and so on.
3. Elements are rendered in the order given, so if the first element is centered
in the screen, other elements aligning themselves to that element will be
aligned relative to screen center.
4. Also, because of this ordering, if using XML to specify this layout, the
element that you will reference (in order to position other view objects)
must be listed in the XML file before you refer to it from the other views via
its reference ID.
24
4. Android – UI - User Interfaces
Common Layouts
4. RelativeLayout
5. The defined RelativeLayout parameters are (android:layout_...) :
• width, height,
• below, above
• alignTop, alignParentTop,
• alignBottom, alignParentBottom
• toLeftOf, toRightOf
• padding [Bottom|Left|Right|Top], and
• margin [Bottom|Left|Right|Top].
For example, assigning the parameter
android:layout_toLeftOf=“@+id/my_button"
to a TextView would place the TextView to the left of the View with the ID my_button
25
4. Android – UI - User Interfaces
Common Layouts
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
android:layout_width="fill_parent"
android:layout_height="wrap_content" RelativeLayout Example
android:background="#ff0000ff"
android:padding="10px" >
The example below shows an XML file
<TextView android:id="@+id/label" and the resulting screen in the UI. Note
android:layout_width="fill_parent" that the attributes that refer to relative
android:layout_height="wrap_content" elements (e.g., layout_toLeft) refer to
android:background="#ffff0077"
the ID using the syntax of a relative
android:text="Type here:" />
resource (@+id/id).
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/label" />
Common Layouts
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" RelativeLayout Example
android:layout_below="@+id/entry"
Cont.
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="OK" />
<Button
android:text="Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/ok"
android:layout_alignTop="@+id/ok“ />
</RelativeLayout>
27
4. Android – UI - User Interfaces
Common Layouts
5. AbsoluteLayout
A layout that lets you specify exact locations (x/y coordinates) of its children.
Absolute layouts are less flexible and harder to maintain than other types of
layouts without absolute positioning.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/myAbsoluteLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="[Link] >
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x=“120px"
android:layout_y=“32px"
>
</Button>
</AbsoluteLayout>
28
4. Android – UI - User Interfaces
[Link]
AbsListView DigitalClock PopupWindow [Link]
[Link] EditText ProgressBar TableRow
AbsoluteLayout ExpandableListView RadioButton [Link]
[Link] ExpandableListContextMenuInfo RadioGroup TabWidget
AbsSeekBar Filter [Link] TextSwitcher
AbsSpinner [Link] RatingBar TextView
AdapterView<T extends Adapter> FrameLayout RelativeLayout [Link]
AdapterContextMenuInfo [Link] [Link] TimePicker
AlphabetIndexer Gallery RemoteViews Toast
AnalogClock [Link] ResourceCursorAdapter ToggleButton
ArrayAdapter<T> GridView ResourceCursorTreeAdapter TwoLineListItem
AutoCompleteTextView HeaderViewListAdapter Scroller VideoView
BaseAdapter HorizontalScrollView ScrollView ViewAnimator
BaseExpandableListAdapter ImageButton SeekBar ViewFlipper
Button ImageSwitcher SimpleAdapter ViewSwitcher
CheckBox ImageView SimpleCursorAdapter ZoomButton
CheckedTextView LinearLayout SimpleCursorTreeAdapter ZoomControls
Chronometer [Link] SimpleExpandableListAdapter
CompoundButton ListView SlidingDrawer
CursorAdapter [Link] Spinner
CursorTreeAdapter MediaController TabHost
DatePicker MultiAutoCompleteTextView [Link]
DialerFilter CommaTokenizer TableLayout
29
4. Android – UI - User Interfaces
JAVA code
pucbli class ….
{
...
...
}
30
4. Android – UI - User Interfaces
setContentView([Link]);
31
4. Android – UI - User Interfaces
The button of our example could now be used, for instance a listener
for the click event could be written as:
[Link](new OnClickListener() {
@Override
public void onClick(View v) {
updateTime();
}
});
32
4. Android – UI - User Interfaces
33
4. Android – UI - User Interfaces
</AbsoluteLayout>
34
4. Android – UI - User Interfaces
37
4. Android – UI - User Interfaces
38
4. Android – UI - User Interfaces
1. Currency calculator
2. Tip Calculator
3. Simple Flashlight
40
4. Android – UI - User Interfaces
41
4. Android – UI - User Interfaces
42
4. Android – UI - User Interfaces
43
4. Android – UI - User Interfaces
44
4. Android – UI - User Interfaces
45
4. Android – UI - User Interfaces
Setting
Hint Capitals & text
spelling
A brief
message box
46
4. Android – UI - User Interfaces
<TextView <Button
android:id="@+id/labelUserName" android:id="@+id/btnBegin"
android:layout_width="227px" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="#ff0000ff" android:text=" Begin Working "
android:padding="3px" android:textSize="14px"
android:text="Enter User Name" android:textStyle="bold"
android:textSize="16sp" >
android:textStyle="bold" </Button>
>
</TextView> </LinearLayout>
47
4. Android – UI - User Interfaces
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
});// onClick
}//onCreate
}//class 49
4. Android – UI - User Interfaces
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
@Override
public void onClick(View v) {
if ([Link]()==[Link]() ){
[Link](getApplicationContext(), "1-Begin", 1).show();
}
if ([Link]()==[Link]() ){
[Link](getApplicationContext(), "2-Exit", 1).show();
}
}//onClick 50
}//class
4. Android – UI - User Interfaces
51
4. Android – UI - User Interfaces
Example 2: CheckBox
Complete code for the checkBox demo (1 of 3)
Layout: [Link]
<CheckBox
<?xml version="1.0" encoding="utf-8"?> android:id="@+id/chkCream"
<LinearLayout android:layout_width="wrap_content"
android:id="@+id/linearLayout" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Cream"
android:layout_height="fill_parent" android:textStyle="bold"
android:background="#ff666666" >
android:orientation="vertical" </CheckBox>
xmlns:android="[Link] <CheckBox
> android:id="@+id/chkSugar"
android:layout_width="wrap_content"
<TextView android:layout_height="wrap_content"
android:id="@+id/labelCoffee" android:text="Sugar"
android:layout_width="fill_parent" android:textStyle="bold"
android:layout_height="wrap_content" >
android:background="#ff993300" </CheckBox>
android:text="What else in you Coffee ?" <Button
android:textStyle="bold" android:id="@+id/btnPay"
> android:layout_width="153px"
</TextView> android:layout_height="wrap_content"
android:text="Pay"
android:textStyle="bold"
>
</Button>
</LinearLayout>
52
4. Android – UI - User Interfaces
Example 2: CheckBox
Complete code for the checkBox demo (2 of 3)
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
//binding XMl controls with Java code
chkCream = (CheckBox)findViewById([Link]);
chkSugar = (CheckBox)findViewById([Link]);
btnPay = (Button) findViewById([Link]);
53
4. Android – UI - User Interfaces
Example 2: CheckBox
Complete code for the checkBox demo (1 of 2)
@Override
public void onClick(View v) {
String msg = "Coffee ";
if ([Link]()) {
msg += " & cream ";
}
if ([Link]()){
msg += " & Sugar";
}
[Link](getApplicationContext(),
msg, Toast.LENGTH_SHORT).show();
//go now and compute cost...
}//onClick
});
}//onCreate
}//class
54
4. Android – UI - User Interfaces
• When several radio buttons live inside a radio group, checking one radio
button unchecks all the others.
• RadioButton inherits from … TextView. Hence, all the standard TextView
properties for font face, style, color, etc. are available for controlling the
look of radio buttons.
• Similarly, you can call isChecked() on a RadioButton to see if it is
selected, toggle() to select it, and so on, like you can with a CheckBox.
55
4. Android – UI - User Interfaces
</LinearLayout>
56
4. Android – UI - User Interfaces
radCoffeeType = (RadioGroup)findViewById([Link]);
radDecaf = (RadioButton)findViewById([Link]);
radExpresso = (RadioButton)findViewById([Link]);
radColombian = (RadioButton)findViewById([Link]);
58
4. Android – UI - User Interfaces
if ([Link]())
msg += " & Sugar";
This UI uses
RadioButtons RadioGroup
and
CheckBoxes
to define choices
Summary of choices
60
UI – Other Features
All widgets extend View therefore they acquire a number of useful View
properties and methods including:
Java methods
[Link]()
[Link]()
[Link]()
[Link]()
61
UI - User Interfaces
Questions ?
62
UI - User Interfaces
Resource: DroidDraw [Link]
63
Android Asset Studio – Beta (Accessed: 18-Jan-2011)
AAS Link: [Link]
Icon Gen [Link]
Pencil 1.2 [Link]
Video: [Link]
Utilities that help in the design and development of Android application user interfaces. This library currently
consists of three individual tools for designers and developers:
1. UI Prototyping Stencils
A set of stencils for the Pencil GUI prototyping tool, which is available as an add-on for Firefox or as a
standalone download.
2. Android Asset Studio
Try out the beta version: Android Asset Studio (shortlink: [Link]
A web-based set of tools for generating graphics and other assets that would eventually be in an Android
application's res/ directory.
Currently available asset generators area available for:
Launcher icons
Menu icons
Tab icons
Notification icons
Support for creation of XML resources and nine-patches is planned for a future release.
3. Android Icon Templates
A set of Photoshop icon templates that follow the icon design guidelines, complementing the
official Android Icon Templates Pack.
64
Questions - Measuring Graphic Elements
Q. What is dpi ?
Stands for dots per inch. You can compute it using the following formula:
dpi = sqrt (width_pixels^2 + height_pixels^2) / diagonal_inches
G1 (base device 320x480) 155.92 dpi (3.7 in diagonally)
Nexus (480x800) 252.15 dpi
Illustration of how the Android platform maps actual screen densities and sizes to generalized
density and size configurations.
65
Questions - Measuring Graphic Elements
Q. What do I gain by using screen densities?
More homogeneous results as shown below
Examples of density
independence on WVGA high
density (left), HVGA medium
density (center), and QVGA
low density (right).
<manifest xmlns:android="[Link]
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
...
</manifest> 66
Questions - Measuring Graphic Elements
Q. Give me an example on how to use dip units.
Assume you design your interface for a G1 phone having 320x480 pixels (Abstracted
LCD density is 160 – See your AVD entry)
You want a button to be hand-placed in the middle of the screen.
You could allocate the 320 horizontal pixels as [ 100 + 120 + 100 ]. The XML would be
<Button>
android:layout_height="wrap_content"
android:layout_width="120dip"
android:layout_x="100dip"
android:layout_y=“240dip"
android:text="Go"
android:id="@+id/btnGo"
</Button>
Instead of using pixels (px) you should use dip. If the application is deployed on a higher
resolution screen (more pixels in 1 dip) the button is still mapped to the middle of the
screen.
67