Figure 11-1: An EditText
view, with the cursor placed in the center (left) and top (right).
Creating the User Input Interface
The most common input type is the EditText
view, used for free-form text entry. Using an EditText
view, you can provide an onscreen keyboard or let the user choose the physical keyboard (if the device provides one) to enter input.
Creating an EditText view
In Chapter 9, you created a view layout XML file, named reminder_edit.xml
, that contained these lines of code:
<EditText android:id=”@+id/title”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
The snippet creates an input mechanism on the screen where the user can type a task title. The EditText
view spans the width of the screen and occupies only as much height as it needs. When the view is selected, Android automatically opens the onscreen keyboard to allow user input.
The previous example takes a minimalistic approach, compared to the following EditText
example, which is also present in the reminder_edit.xml
layout file:
<EditText android:id=”@+id/body” android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:minLines=”5”
android:scrollbars=”vertical”
android:gravity=”top” />
This code creates the body description text of the task. The layout width and height are the same as in the EditText
view in the previous example, and the EditText
view spans the width of the screen. These three properties outline the differences in this EditText
definition:
minLines
: Specifies the height of the EditText
view. Because the EditText
view is a subclass of the TextView
object, they share this property. This code specifies a minimum of five lines for the EditText
object onscreen so that the view resembles a text input mechanism for long messages.
Compare this view to the body portion of any e-mail client, and you can see that they’re much the same — the body is much larger than the subject. In this case, the body is much larger than the title.
scrollbars
: Defines which scroll bars should be present when the text overflows the available input area; specifies vertical scroll bars on the side of the EditText
view.
gravity
: Aligns text (by default) to the middle of the view when the user places focus into an EditText
field (as shown on the left in Figure 11-1), though it isn’t what users would expect when they work with a multiline input mechanism. To position the cursor at the top of the EditText
view, as users would reasonably expect, you must set the gravity of the EditText
view to top
, to force the text to gravitate to the top of the EditText
input as shown on the right in Figure 11-1.
Figure 11-1: An EditText
view, with the cursor placed in the center (left) and top (right).
Displaying an onscreen keyboard
The EditText
view is responsible for the onscreen keyboard display. Because some devices have no physical keyboard, an onscreen keyboard must be present for interaction with the input mechanisms. One property that the EditText
view provides is a way to manipulate the visual aspect of the onscreen keyboard.
You adjust the onscreen keyboard because different EditText
input types might need different keys. For example, if the EditText
is a phone number, the onscreen keyboard should display only numbers. If the EditText
value is an e-mail address, however, the onscreen keyboard should display common e-mail style attributes — such as the at (@) symbol.
You can configure the way the onscreen keyboard looks by using the inputType
property on the EditText
view. For example, if you set android:inputType=”number”
on the body EditText
, the keyboard displays number keys instead of letter keys, as shown in Figure 11-2.
Figure 11-2: Keyboard customized for number entry.
The inputType
property has too many options to cover in this book, but you can examine the full list at http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
.