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.

remember.eps In case you’re familiar with other programming platforms, a text box performs the same function as an EditText view.

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:

check.png 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.

tip.eps 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.

check.png 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.

check.png 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).

9781118417454-fg1101.eps

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.

remember.eps Configuring the onscreen keyboard properly can increase the usability of your application.

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.

9781118417454-fg1102.tif

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 .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset