Specifying the Input Method Type

This lesson teaches you to

Every text field expects a certain type of text input, such as an
email address, phone number, or just plain text. So it's important
that you specify the input type for each text field in your app
so the system displays the appropriate soft input method (such as an on-screen keyboard).

Beyond the type of buttons available with an input method, you should specify
behaviors such as whether the input method provides spelling suggestions,
capitalizes new sentences, and replaces the carriage return button with an
action button such as a Done or Next.
This lesson shows how to specify these characteristics.

Enable Spelling Suggestions and Other Behaviors

The android:inputType attribute allows you to specify various behaviors for the
input method. Most importantly, if your text field is intended for basic text input (such
as for a text message), you should enable auto spelling correction with the
"textAutoCorrect" value.

You can combine different behaviors and input method styles with the
android:inputType attribute. For example,
here's how to create a text field that capitalizes the first word of a sentence
and also auto-corrects misspellings:

Specify the Input Method Action

Most soft input methods provide a user action button in the
bottom corner that's appropriate for the current text field.
By default, the system uses this button for either a Next or
Done action unless your text field allows multi-line text (such as with
android:inputType="textMultiLine"), in which case the action button
is a carriage return.
However, you can specify additional actions that might be more appropriate for your
text field, such as Send or Go.

To specify the keyboard action button, use the android:imeOptions
attribute with an action value such as "actionSend" or
"actionSearch". For example:

Figure 4. The Send button appears when you declare
android:imeOptions="actionSend".

Provide Auto-complete Suggestions

If you want to provide suggestions to users as they type, you can use a
subclass of EditText called AutoCompleteTextView. To implement auto-complete, you must
specify an Adapter that provides the text suggestions.
There are several kinds of adapters available, depending on where the data is
coming from, such as from a database or an array.

In your Activity or Fragment, use
the following code to specify the adapter that supplies the suggestions:

// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
// Get the string array
String[] countries = getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);

Here, a new ArrayAdapter is initialized to bind each
item in the countries_array string array to a TextView that exists in the simple_list_item_1
layout (this is a layout provided by Android that provides a standard
appearance for text in a list).