This chapter is from the book

This chapter is from the book

Users interact with Android devices in many ways, including using keyboards, touch-screen gestures, and even voice. Different devices support different input methods and have different hardware. For example, certain devices have hardware keyboards, and others rely only on software keyboards. In this chapter, you will learn about the different input methods available to developers and how you can use them to great effect within your applications.

Working with Textual Input Methods

The Android SDK includes input method framework classes that enable interested developers to use powerful input methods and create their own input methods, such as custom software keyboards and other Input Method Editors (IMEs). Users can download custom IMEs to use on their devices. For example, there’s nothing stopping a developer from creating a custom keyboard with Lord of the Rings–style Elvish characters, smiley faces, or Greek symbols.

TIP

Most device settings related to input methods are available under the Settings, Language & input menu. Here, users can select the language, configure the custom user dictionary, and make changes to how their keyboards function.

The Android SDK also includes a number of other text input utilities that might benefit application users, such as text prediction, dictionaries, and the clipboard framework, which can be used to enable sophisticated cut-and-paste features in your application for text and much more.

Working with Software Keyboards

Because text input methods are locale-based (different countries use different alphabets and keyboards) and situational (numeric versus alphabetic versus special keys), the Android platform has trended toward software keyboards as opposed to relying on hardware manufacturers to deliver specialized hardware keyboards.

Choosing the Appropriate Software Keyboard

The Android platform has a number of software keyboards available for use. One of the easiest ways to enable your users to enter data efficiently is to specify the type of input expected in each text input field.

TIP

Many of the code examples provided in this section are taken from the SimpleTextInputTypes application. The source code for this application is provided for download on the book’s website.

For example, to specify an EditText that should take only capitalized textual input, you can set the inputType attribute as follows:

Figure 8.1 shows a number of EditText controls with different inputType configurations.

Figure 8.1 EditText controls with different input types.

The input type dictates which software keyboard is used by default, and it enforces appropriate rules, such as limiting input to certain characters. Figure 8.2 (left) illustrates what the software keyboard looks like for an EditText control with its inputType attribute set to all capitalized text input. Note that the software keyboard keys are all capitalized. If you were to set the inputType to textCapWords instead, the keyboard would switch to lowercase after the first letter of each word and then back to uppercase after a space. Figure 8.2 (middle) illustrates what the software keyboard looks like for an EditText control with its inputType attribute set to number. Figure 8.2 (right) illustrates what the software keyboard looks like for an EditText control with its inputType attribute set to textual input, where each sentence begins with a capital letter and the text can be multiple lines.

Depending on the user’s keyboard settings (specifically, if the user has enabled the Show correction suggestions and Auto-correction options in the Android Keyboard settings of the device), the user might also see suggested words or spelling fixes while typing. For a complete list of inputType attribute values and their uses, see http://developer.android.com/reference/android/R.attr.html#inputType.

TIP

You can also have your Activity react to the display of software keyboards (to adjust where fields are displayed, for example) by requesting the WindowManager as a system Service and modifying the layout parameters associated with the softInputMode field.

For more fine-tuned control over input methods, see the android.view.inputmethod.InputMethodManager class.

Providing Custom Software Keyboards

If you are interested in developing your own software keyboards, we highly recommend the following references:

IMEs are implemented as an Android Service. Begin by reviewing the Android packages called android.inputmethodservice and android.view.inputmethod, which can be used to implement custom input methods.

The SoftKeyboard legacy sample application in the Android SDK provides an implementation of a software keyboard.

Working with Text Prediction and User Dictionaries

Text prediction is a powerful and flexible feature that is available on Android devices. We’ve already talked about many of these technologies in other parts of this book, but they merit mentioning in this context as well:

In Introduction to Android Application Development: Android Essentials, Fourth Edition, you learned how to use AutoCompleteTextView and MultiAutoCompleteTextView controls to help users input common words and strings.

In Chapter 3, “Leveraging SQLite Application Databases,” you learned how to tie an AutoCompleteTextView control to an underlying SQLite database table.

In Introduction to Android Application Development: Android Essentials, Fourth Edition, you learned about the UserDictionary content provider (android.provider.UserDictionary), which can be used to add words to the user’s custom dictionary of commonly used words.

Using the Clipboard Framework

On Android devices running Android 3.0 and higher (API Level 11), developers can access the clipboard to perform copy and paste actions. Previous to this, the clipboard had no public API. To leverage the clipboard in your applications, you need to use the clipboard framework of the Android SDK. You can copy and paste different data structures—everything from text to references to files to application shortcuts—as Intent objects. The clipboard holds only a single set of clipped data at a time, and the clipboard is shared across all applications, so you can easily copy and paste content between applications.

Copying Data to the System Clipboard

To save data to the system clipboard, call getSystemService() and request the clipboard Service’s ClipboardManager (android.content.ClipboardManager). Then, create a ClipData (android.content.ClipData) object and populate it with the data you want to save to the clipboard. Finally, commit the clip using the ClipboardManager class method setPrimaryClip().

Pasting Data from the System Clipboard

To retrieve data from the system clipboard, call getSystemService() and request the clipboard Service’s ClipboardManager (android.content.ClipboardManager). You can determine whether the clipboard contains data by using the hasPrimaryClip() method. After you have determined whether there is valid data in the system clipboard, you can inspect its description and type and ultimately retrieve the ClipData object using the getPrimaryClip() method.