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 EditView widget, which is a subclass of the TextView used for labels.

Along with the standard TextView properties (e.g., android:textStyle), EditView has many others that will be useful for you in constructing fields, including:

• android:autoText, to control if the field should provide automatic spelling assistance

• android:capitalize, to control if the field should automatically capitalize the first letter of entered text (e.g., first name, city)

• android:digits, to configure the field to accept only certain digits

• android :singleLine, to control if the field is for single-line input or multiple-line input (e.g., does <Enter> move you to the next widget or add a newline?)

Beyond those, you can configure fields to use specialized input methods, such as android:numeric for numeric-only input, android:password for

shrouded password input, and android:phoneNumber for entering in phone numbers. If you want to create your own input method scheme (e.g., postal codes, Social Security numbers), you need to create your own implementation of the inputMethod interface, then configure the field to use it via android:inputMethod. You can see an example of this in the appendix discussing the TourIt sample application.

For example, from the Field project, here is an XML layout file showing an EditView:

<?xml version="1.0" encoding="utf-8"?>

<EditText xmlns:android="" android:id="(S+id/field" android:layout_width="fill_parent" android:layout_height="fill_parent" android:singleLine="false" />

Note that android: singleLine is false, so users will be able to enter in several lines of text.

For this project, the file populates the input field with some prose:


import; import android.os.Bundle; import android.widget.EditText;

public class FieldDemo extends Activity {

(SOverride public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

EditText fld=(EditText)findViewById(; 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 " + "");

The result, once built and installed into the emulator, is:


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 s/LICENSE-2.0

Figure 5. The FieldDemo sample application

NOTE: Android's emulator only allows one application in the launcher per unique Java package. Since all the demos in this chapter share the package, if you have the LabelDemo application installed, you will not see the FieldDemo application in the launcher. To remove the LabelDemo application - or any application - use adb shell "rm /data/app/...", where ... is the name of the application's APK file (e.g., LabelDemo.apk). Then, reinstall the formerly-hidden application, and it will show up in the launcher.

Another flavor of field is one that offers auto-completion, to help users supply a value without typing in the whole text. That is provided in Android as the AutoCompleteTextView widget, discussed in greater detail later in this book.

Self Publishing

Self Publishing

Have you always wanted to write your own book and get it published? Discover How to Write, Print Sell Your Own Book! Have you always wanted to write your own book and get it published? If you have written a book or even if you have a yen to be a writer, you are probably already aware of the competition in the writing field.

Get My Free Ebook

Post a comment