From 86efa014fce2b7514f114bc7491082bee5e2c9f1 Mon Sep 17 00:00:00 2001
From: David Friedman
- An input method editor (IME) is a user control that enables users to enter text. Android
- provides an extensible input method framework that allows applications to provide users
- alternative input methods, such as on-screen keyboards or even speech input. Once installed,
- users can select which IME they want to use from the system settings and use it across the
+ An input method editor (IME) is a user control that enables users to enter text. Android
+ provides an extensible input method framework that allows applications to provide users
+ alternative input methods, such as on-screen keyboards or even speech input. Once installed,
+ users can select which IME they want to use from the system settings and use it across the
entire system; only one IME may be enabled at a time.
To add an IME to the Android system, you create an Android application
- containing a class that extends {@link android.inputmethodservice.InputMethodService}. In
+ containing a class that extends {@link android.inputmethodservice.InputMethodService}. In
addition, you usually create a "settings" activity that passes options to the IME
service. You can also define a settings UI that's displayed as part of the system settings.
This article covers the following:
- If you haven't worked with IMEs before, you should read the introductory article
+ If you haven't worked with IMEs before, you should read the introductory article
Onscreen Input Methods first.
Also, the Soft Keyboard sample app included in the SDK contains sample code that you can modify
to start building your own IME.
@@ -59,16 +74,16 @@ page.tags="ime","keyboard","inputmethodservice"
In the Android system, an IME is an Android application that contains a special IME service.
- The application's manifest file must declare the service, request the necessary permissions,
- provide an intent filter that matches the action
The following snippet declares IME service. It requests the permission {@link
- android.Manifest.permission#BIND_INPUT_METHOD} to allow the service to connect the IME to
- the system, sets up an intent filter that matches the action
+ android.Manifest.permission#BIND_INPUT_METHOD} to allow the service to connect the IME to
+ the system, sets up an intent filter that matches the action
In This Document
+
+
See also
-
Declaring IME Components in the Manifest
action.view.InputMethod, and
+ The application's manifest file must declare the service, request the necessary permissions,
+ provide an intent filter that matches the action action.view.InputMethod, and
provide metadata that defines characteristics of the IME. In addition, to provide a settings
interface that allows the user to modify the behavior of the IME, you can define a "settings"
activity that can be launched from System Settings.
android.view.InputMethod, and defines metadata for the IME:
@@ -88,7 +103,7 @@ page.tags="ime","keyboard","inputmethodservice"
for the IME application:
<!-- Optional: an activity for controlling the IME settings -->
- <activity android:name="FastInputIMESettings"
+ <activity android:name="FastInputIMESettings"
android:label="@string/fast_input_settings">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@@ -105,12 +120,12 @@ page.tags="ime","keyboard","inputmethodservice"
handling keyboard characters.
- The central part of an IME is a service component, a class that extends
- {@link android.inputmethodservice.InputMethodService}. In addition to implementing the
- normal service lifecycle, this class has callbacks for providing your IME's UI, handling user
+ The central part of an IME is a service component, a class that extends
+ {@link android.inputmethodservice.InputMethodService}. In addition to implementing the
+ normal service lifecycle, this class has callbacks for providing your IME's UI, handling user
input, and delivering text to the field that currently has focus. By default, the
- {@link android.inputmethodservice.InputMethodService} class provides most of the implementation
- for managing the state and visibility of the IME and communicating with the current
+ {@link android.inputmethodservice.InputMethodService} class provides most of the implementation
+ for managing the state and visibility of the IME and communicating with the current
input field.
@@ -122,13 +137,13 @@ page.tags="ime","keyboard","inputmethodservice"
Defines the communication channel from an {@link android.view.inputmethod.InputMethod}
back to the application that is receiving its input. You use it to read text around the
cursor, commit text to the text box, and send raw key events to the application.
- Applications should extend this class rather than implementing the base interface
+ Applications should extend this class rather than implementing the base interface
{@link android.view.inputmethod.InputConnection}.
The input view is the UI where the user inputs text, in the form of keyclicks, handwriting or - gestures. When the iIME is displayed for the first time, the system calls the + gestures. When the iIME is displayed for the first time, the system calls the {@link android.inputmethodservice.InputMethodService#onCreateInputView()} callback. In your implementation of this method, you create the layout you want to display in the IME window and return the layout to the system. This snippet is an example of implementing the {@link android.inputmethodservice.InputMethodService#onCreateInputView()} method:
- @Override
- public View onCreateInputView() {
- MyKeyboardView inputView =
+ @Override
+ public View onCreateInputView() {
+ MyKeyboardView inputView =
(MyKeyboardView) getLayoutInflater().inflate( R.layout.input, null);
-
- inputView.setOnKeyboardActionListener(this); inputView.setKeyboard(mLatinKeyboard);
-
- return mInputView;
- }
+
+ inputView.setOnKeyboardActionListener(this); inputView.setKeyboard(mLatinKeyboard);
+
+ return mInputView;
+ }
- In this example, {@code MyKeyboardView} is an instance of a custom implementation of - {@link android.inputmethodservice.KeyboardView} that renders a - {@link android.inputmethodservice.Keyboard}. If you’re building a traditional QWERTY keyboard, - see the Soft Keyboard sample + In this example, {@code MyKeyboardView} is an instance of a custom implementation of + {@link android.inputmethodservice.KeyboardView} that renders a + {@link android.inputmethodservice.Keyboard}. If you’re building a traditional QWERTY keyboard, + see the Soft Keyboard sample app for an example of how to extend the {@link android.inputmethodservice.KeyboardView} class.
The candidates view is the UI where the IME displays potential word corrections or - suggestions for the user to select. In the IME lifecycle, the system calls - {@link android.inputmethodservice.InputMethodService#onCreateCandidatesView()} when it's ready + suggestions for the user to select. In the IME lifecycle, the system calls + {@link android.inputmethodservice.InputMethodService#onCreateCandidatesView()} when it's ready to display the candidate view. In your implementation of this method, return a layout that shows word suggestions, or return null if you don’t want to show anything (a null response is the default behavior, so you don’t have to implement this if you don’t provide suggestions).
- For an example implementation that provides user suggestions, see the - Soft Keyboard sample + For an example implementation that provides user suggestions, see the + Soft Keyboard sample app.
- When an input field receives focus and your IME starts, the system calls + When an input field receives focus and your IME starts, the system calls {@link android.inputmethodservice.InputMethodService#onStartInputView(EditorInfo, boolean) - onStartInputView()}, passing in an {@link android.view.inputmethod.EditorInfo} object that - contains details about the input type and other attributes of the text field. In this object, + onStartInputView()}, passing in an {@link android.view.inputmethod.EditorInfo} object that + contains details about the input type and other attributes of the text field. In this object, the {@link android.view.inputmethod.EditorInfo#inputType} field contains the text field's input type.
@@ -223,7 +238,7 @@ page.tags="ime","keyboard","inputmethodservice" this:-inputType & InputType.TYPE_MASK_CLASS +inputType & InputType.TYPE_MASK_CLASS
The input type bit pattern can have one of several values, including: @@ -248,7 +263,7 @@ The input type bit pattern can have one of several values, including:
- These constants are described in more detail in the reference documentation for + These constants are described in more detail in the reference documentation for {@link android.text.InputType}.
@@ -287,8 +302,8 @@ The input type bit pattern can have one of several values, including:
As the user inputs text with your IME, you can send text to the application by sending individual key events or by editing the text around the cursor in the application's text - field. In either case, you use an instance of {@link android.view.inputmethod.InputConnection} - to deliver the text. To get this instance, call + field. In either case, you use an instance of {@link android.view.inputmethod.InputConnection} + to deliver the text. To get this instance, call {@link android.inputmethodservice.InputMethodService#getCurrentInputConnection InputMethodService.getCurrentInputConnection()}.
@@ -336,18 +351,18 @@ The input type bit pattern can have one of several values, including:
InputConnection ic = getCurrentInputConnection();
-
+
ic.deleteSurroundingText(4, 0);
-
+
ic.commitText("Hello", 1);
-
+
ic.commitText("!", 1);
If your IME does text prediction or requires multiple steps to compose a glyph or word, you can show the progress in the text field until the user commits the word, and then you - can replace the partial composition with the completed text. You may give special treatment to + can replace the partial composition with the completed text. You may give special treatment to the text by adding a "span" to it when you pass it to InputConnection#setComposingText().
@@ -385,10 +400,10 @@ The input type bit pattern can have one of several values, including: selection during composition. You may also want to trap the back key to dismiss any popups originating from the input method window.
- To intercept hardware keys, override + To intercept hardware keys, override {@link android.inputmethodservice.InputMethodService#onKeyDown(int, KeyEvent) onKeyDown()} - and {@link android.inputmethodservice.InputMethodService#onKeyUp(int, KeyEvent) onKeyUp()}. - See the Soft Keyboard sample + and {@link android.inputmethodservice.InputMethodService#onKeyUp(int, KeyEvent) onKeyUp()}. + See the Soft Keyboard sample app for an example.
@@ -396,7 +411,7 @@ The input type bit pattern can have one of several values, including:
- Subtypes allow the IME to expose multiple input modes and languages supported by an IME. A + Subtypes allow the IME to expose multiple input modes and languages supported by an IME. A subtype can represent:
Subtype information is used for an IME switcher dialog that's available from the notification bar and also for IME settings. The information also allows the framework to bring up a - specific subtype of an IME directly. When you build an IME, use the subtype facility, because + specific subtype of an IME directly. When you build an IME, use the subtype facility, because it helps the user identify and switch between different IME languages and modes.
You define subtypes in one of the input method's XML resource files, using the
- <subtype> element. The following snippet defines an IME with two
- subtypes: a keyboard subtype for the US English locale, and another keyboard subtype for the
+ <subtype> element. The following snippet defines an IME with two
+ subtypes: a keyboard subtype for the US English locale, and another keyboard subtype for the
French language locale for France:
@@ -457,8 +472,8 @@ The input type bit pattern can have one of several values, including:
android:imeSubtypeMode="keyboard" />
- The next snippet is part of the IME's strings.xml file. The string
- resource label_subtype_generic, which is used by the input method UI definition to
+ The next snippet is part of the IME's strings.xml file. The string
+ resource label_subtype_generic, which is used by the input method UI definition to
set the subtype's label, is defined as:
@@ -487,9 +502,9 @@ The input type bit pattern can have one of several values, including:Choosing IME subtypes from System Settings
A user can control how subtypes are used in the “Language & input” settings panel in the - System Settings area. In the Soft Keyboard sample, the file -
InputMethodSettingsFragment.javacontains an implementation that - facilitates a subtype enabler in the IME settings. Please refer to the SoftKeyboard sample in + System Settings area. In the Soft Keyboard sample, the file +InputMethodSettingsFragment.javacontains an implementation that + facilitates a subtype enabler in the IME settings. Please refer to the SoftKeyboard sample in the Android SDK for more information about how to support Input Method Subtypes in your IME.Figure 6. Choosing a language for the IME. +
General IME Considerations
Here are some other things to consider as you're implementing your IME: @@ -506,22 +522,22 @@ The input type bit pattern can have one of several values, including: Provide a way for users to set options directly from the IME's UI.