MOBILE

Beginning Android 3 : The Input Method Framework - Tailored to Your Needs

8/6/2011 3:43:18 PM
Android 1.5 introduced the input method framework (IMF), which is commonly referred to as soft keyboards. However, this term is not necessarily accurate, as IMF could be used for handwriting recognition or other means of accepting text input via the screen.

1. Keyboards, Hard and Soft

Some Android devices have a hardware keyboard that is visible some of the time (when it is slid out). A few Android devices have a hardware keyboard that is always visible (so-called "bar" or "slab" phones). Most Android devices, though, have no hardware keyboard at all. The IMF handles all of these scenarios.

In short, if there is no hardware keyboard, an input method editor (IME) will be available to the user when they tap an enabled EditText. If the default functionality of the IME is what you want to offer, you don't need to make any code changes to your application. Fortunately, Android is fairly smart about guessing what you want, so you may simply need to test with the IME and make no specific code changes.

But the IME may not quite behave how you would like it to for your application. For example, in the Basic/Field sample project, the FieldDemo activity has the IME overlaying the multiple-line EditText, as shown in Figure 1. It would be nice to have more control over how this appears, and to be able to control other behavior of the IME. Fortunately, the IMF as a whole gives you many options for this.

Figure 1. The input method editor, as seen in the FieldDemo sample application


2. Tailored to Your Needs

Android 1.1 and earlier offered many attributes on EditText widgets to control their style of input, such as android:password to indicate a field should be for password entry (shrouding the password keystrokes from prying eyes). Starting in Android 1.5, with the IMF, many of these attributes have been combined into a single android:inputType attribute.

The android:inputType attribute takes a class plus modifiers, in a pipe-delimited list (where | is the pipe character). The class generally describes what the user is allowed to input, and this determines the basic set of keys available on the soft keyboard. The available classes are as follows:

  • text (the default)

  • number

  • phone

  • datetime

  • date

  • time

Many of these classes offer one or more modifiers to further refine what the user will be allowed to enter. To get a better understanding of how these modifiers work, take a look at the res/layout/main.xml file from the InputMethod/IMEDemo1 project:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:text="No special rules:"
/>
<EditText
/>
</TableRow>
<TableRow>
<TextView
android:text="Email address:"
/>
<EditText
android:inputType="text|textEmailAddress"
/>
</TableRow>
<TableRow>
<TextView
android:text="Signed decimal number:"
/>
<EditText
android:inputType="number|numberSigned|numberDecimal"
/>
</TableRow>
<TableRow>
<TextView
android:text="Date:"
/>
<EditText
android:inputType="date"
/>
</TableRow>
<TableRow>
<TextView
android:text="Multi-line text:"
/>
<EditText
android:inputType="text|textMultiLine|textAutoCorrect"
android:minLines="3"
android:gravity="top"
/>
</TableRow>
</TableLayout>


This shows a TableLayout containing five rows, each demonstrating a slightly different flavor of EditText:

  • The first row has no attributes at all on the EditText, meaning you get a plain text-entry field.

  • The second row has android:inputType = "text|textEmailAddress", meaning it is a text-entry field that specifically seeks an e-mail address.

  • The third row allows for signed decimal numeric input, via android:inputType = "number|numberSigned|numberDecimal".

  • The fourth row is set up to allow for data entry of a date (android:inputType = "date").

  • The last allows for multiline input with autocorrection of probable spelling errors (android:inputType = "text|textMultiLine|textAutoCorrect").

The class and modifiers tailor the keyboard. So, a plain text-entry field results in a plain soft keyboard, as shown in Figure 2.

Figure 2. A standard input method editor (a.k.a., soft keyboard)

An e-mail address field might put the @ symbol on the soft keyboard, at the cost of a smaller spacebar, as shown in Figure 3.

Figure 3. The input method editor for e-mail addresses

Note, though, that this behavior is specific to the IME. Some editors might put the @ symbol on the primary keyboard for an e-mail field. Some might put a .com button on the primary keyboard. Some might not react at all. It is up to the implementation of the IME—all you can do is supply the hint.

Number and date fields restrict the keys to numeric keys, plus a set of symbols that may or may not be valid on a given field, as shown in Figure 4.

Figure 4. The input method editor for signed decimal numbers

These are just a few examples of the possible IMEs. By choosing the appropriate android:inputType, you can give users a soft keyboard that best suits the type of data they should be entering.

Other  
  •  Beginning Android 3 : Working with Containers - Scrollwork
  •  Mobile Application Security : SMS Security - Protocol Attacks (part 2)
  •  Mobile Application Security : SMS Security - Protocol Attacks (part 1)
  •  Mobile Application Security : SMS Security - Overview of Short Message Service
  •  iPad SDK : Popovers - The Font Name Popover (part 2)
  •  iPad SDK : Popovers - The Font Name Popover (part 1)
  •  Beginning Android 3 : Working with Containers - Tabula Rasa
  •  Beginning Android 3 : Working with Containers - LinearLayout Example & The Box Model
  •  iPhone Application Development : Reading and Writing User Defaults (part 2) - Implementing System Settings
  •  iPhone Application Development : Reading and Writing User Defaults (part 1) - Creating Implicit Preferences
  •  - Mobile Application Security : SMS Security - Overview of Short Message Service
  •  - Mobile Application Security : Bluetooth Security - Bluetooth Security Features
  •  Integrating Your Application with Windows Phone 7
  •  Introducing Windows Phone 7 Photo Features (part 2) - Using a Chooser to Open Photos & Saving Photos to the Phone
  •  Introducing Windows Phone 7 Photo Features (part 1) - Using a Chooser to Take Photos
  •  Mobile Application Security : Bluetooth Security - Bluetooth Technical Architecture
  •  Mobile Application Security : Bluetooth Security - Overview of the Technology
  •  Windows Phone 7 Development : Push Notifications - Implementing Cloud Service to Track Push Notifications
  •  Windows Phone 7 Development : Push Notifications - Implementing Raw Notifications
  •  Windows Phone 7 Development : Push Notifications - Implementing Tile Notifications
  •  
    Top 10
    A Look At Truecrypt The Open Source Security Tool
    Price Of Piracy
    Acer Aspire 5600U 23" Touchscreen All-in-One PC
    Zalman FX100-Cube Fanless Cooler
    Devolo dLAN LiveCam Starter Kit
    Has Apple Lost It? (Part 2)
    Has Apple Lost It? (Part 1)
    Sony Computer Entertainment (Part 3)
    Sony Computer Entertainment (Part 2)
    Sony Computer Entertainment (Part 1)
    Most View
    $150 Android HD/3D Player
    Registering CLR Assemblies for Use with SQL Server
    SQL Server 2005 : Advanced OLAP - Key Performance Indicators (part 2) - KPI Queries in Management Studio
    iphone Programming : Integrating Your Application - Application Preferences
    Algorithms for Compiler Design: ACTIVATION OF THE PROCEDURE AND THE ACTIVATION RECORD
    Personalizing Windows 7 (part 2) - Choosing Your Desktop Background
    Create Stunning Zoom Burst Images (Part 1)
    Pioneer HTIB Surround Sound Systems With Prices Ranging From $360
    Programming with DirectX : Additional Texture Mapping - Alpha Mapping
    Defensive Database Programming with SQL Server: The Ticket-Tracking System (part 1) - Enforcing business rules using constraints only
    Can You Go Viral?
    Complete Guide for Your PC & How to Buy It (Part 3) - Lenovo ThinkPad Edge E520 & Toshiba Portégé Z830-104
    Biggest tips guide ever! (Part 8)
    Western Digital My Book Live Duo - Full, Double Lives
    Choosing The Right Parts For Your Build (Part 5) - Choosing your case & Picking the right storage
    Top 10 Smartphones With Remarkable Cameras
    MSI Z77 MPOWER - Enhances Stability
    Canon EOS 6D - A Beginner-Friendly Full-Frame Camera
    Improve Your Mac (Part 10) - Add Video To Pages Projects
    SQL Server 2008 : Managing Query Performance - Finding Similar Queries