1. Fleeting Images
Android has two widgets to help you embed images in your activities: ImageView and ImageButton. As the names suggest, they are image-based analogues to TextView and Button, respectively.
Each widget takes an android:src attribute
(in an XML layout) to specify which picture to use. These attributes
usually reference a drawable resource.
ImageButton, a subclass of ImageView, mixes in the standard Button behaviors, for responding to clicks and whatnot. For example, take a peek at the main.xml layout from the Basic/ImageView sample project:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:src="@drawable/molecule"
/>
The result, just using the code-generated activity, is simply the image, as shown in Figure 1.
2. Fields of Green...or Other Colors
Along with buttons and labels, fields are the third anchor of most GUI toolkits. In Android, they are implemented via the EditText widget, which is a subclass of the TextView used for labels.
Along with the standard TextView properties (e.g., android:textStyle), EditText has many other properties that will be useful to you in constructing fields, including the following:
android:autoText: Controls if the field should provide automatic spelling assistance
android:capitalize: Controls if the field should automatically capitalize the first letter of entered text (e.g., in name and city fields)
android:digits: Configures the field to accept only certain digits
android:singleLine:
Controls if the field is for single-line input or multiple-line input
(e.g., does pressing Enter move you to the next widget or add a
newline?)
Most of the preceding properties are also available from the new android:inputType attribute, added in Android 1.5 as part of adding "soft keyboards" to Android .
For example, from the Basic/Field project, here is an XML layout file showing an EditText widget:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/field"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="false"
/>
Note that android:singleLine is set to "false", so users will be able to enter several lines of text.
For this project, the FieldDemo.java file populates the input field with some prose:
package com.commonsware.android.field;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class FieldDemo extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
EditText fld=(EditText)findViewById(R.id.field);
fld.setText("Licensed under the Apache License, Version 2.0 " +
"(the \"License\"); you may not use this file " +
"except in compliance with the License. You may " +
"obtain a copy of the License at " +
"http://www.apache.org/licenses/LICENSE-2.0");
}
}
The result, once built and installed into the emulator, is shown in Figure 2.
Another flavor of field is one that offers
autocompletion, to help users supply a value without typing in the whole
text. That is provided in Android as the AutoCompleteTextView widget.