Most visited

Recently visited

Added in API level 3

InputMethodService

public class InputMethodService
extends AbstractInputMethodService

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ android.inputmethodservice.AbstractInputMethodService
           ↳ android.inputmethodservice.InputMethodService


InputMethodService provides a standard implementation of an InputMethod, which final implementations can derive from and customize. See the base class AbstractInputMethodService and the InputMethod interface for more information on the basics of writing input methods.

In addition to the normal Service lifecycle methods, this class introduces some new specific callbacks that most subclasses will want to make use of:

An input method has significant discretion in how it goes about its work: the InputMethodService provides a basic framework for standard UI elements (input view, candidates view, and running in fullscreen mode), but it is up to a particular implementor to decide how to use them. For example, one input method could implement an input area with a keyboard, another could allow the user to draw text, while a third could have no input area (and thus not be visible to the user) but instead listen to audio and perform text to speech conversion.

In the implementation provided here, all of these elements are placed together in a single window managed by the InputMethodService. It will execute callbacks as it needs information about them, and provides APIs for programmatic control over them. They layout of these elements is explicitly defined:

Soft Input View

Central to most input methods is the soft input view. This is where most user interaction occurs: pressing on soft keys, drawing characters, or however else your input method wants to generate text. Most implementations will simply have their own view doing all of this work, and return a new instance of it when onCreateInputView() is called. At that point, as long as the input view is visible, you will see user interaction in that view and can call back on the InputMethodService to interact with the application as appropriate.

There are some situations where you want to decide whether or not your soft input view should be shown to the user. This is done by implementing the onEvaluateInputViewShown() to return true or false based on whether it should be shown in the current environment. If any of your state has changed that may impact this, call updateInputViewShown() to have it re-evaluated. The default implementation always shows the input view unless there is a hard keyboard available, which is the appropriate behavior for most input methods.

Candidates View

Often while the user is generating raw text, an input method wants to provide them with a list of possible interpretations of that text that can be selected for use. This is accomplished with the candidates view, and like the soft input view you implement onCreateCandidatesView() to instantiate your own view implementing your candidates UI.

Management of the candidates view is a little different than the input view, because the candidates view tends to be more transient, being shown only when there are possible candidates for the current text being entered by the user. To control whether the candidates view is shown, you use setCandidatesViewShown(boolean). Note that because the candidate view tends to be shown and hidden a lot, it does not impact the application UI in the same way as the soft input view: it will never cause application windows to resize, only cause them to be panned if needed for the user to see the current focus.

Fullscreen Mode

Sometimes your input method UI is too large to integrate with the application UI, so you just want to take over the screen. This is accomplished by switching to full-screen mode, causing the input method window to fill the entire screen and add its own "extracted text" editor showing the user the text that is being typed. Unlike the other UI elements, there is a standard implementation for the extract editor that you should not need to change. The editor is placed at the top of the IME, above the input and candidates views.

Similar to the input view, you control whether the IME is running in fullscreen mode by implementing onEvaluateFullscreenMode() to return true or false based on whether it should be fullscreen in the current environment. If any of your state has changed that may impact this, call updateFullscreenMode() to have it re-evaluated. The default implementation selects fullscreen mode when the screen is in a landscape orientation, which is appropriate behavior for most input methods that have a significant input area.

When in fullscreen mode, you have some special requirements because the user can not see the application UI. In particular, you should implement onDisplayCompletions(CompletionInfo[]) to show completions generated by your application, typically in your candidates view like you would normally show candidates.

Generating Text

The key part of an IME is of course generating text for the application. This is done through calls to the InputConnection interface to the application, which can be retrieved from getCurrentInputConnection(). This interface allows you to generate raw key events or, if the target supports it, directly edit in strings of candidates and committed text.

Information about what the target is expected and supports can be found through the EditorInfo class, which is retrieved with getCurrentInputEditorInfo() method. The most important part of this is EditorInfo.inputType; in particular, if this is EditorInfo.TYPE_NULL, then the target does not support complex edits and you need to only deliver raw key events to it. An input method will also want to look at other values here, to for example detect password mode, auto complete text views, phone number entry, etc.

When the user switches between input targets, you will receive calls to onFinishInput() and onStartInput(EditorInfo, boolean). You can use these to reset and initialize your input state for the current target. For example, you will often want to clear any input state, and update a soft keyboard to be appropriate for the new inputType.

Summary

Nested classes

class InputMethodService.InputMethodImpl

Concrete implementation of AbstractInputMethodService.AbstractInputMethodImpl that provides all of the standard behavior for an input method. 

class InputMethodService.InputMethodSessionImpl

Concrete implementation of AbstractInputMethodService.AbstractInputMethodSessionImpl that provides all of the standard behavior for an input method session. 

class InputMethodService.Insets

Information about where interesting parts of the input method UI appear. 

XML attributes

android:imeExtractEnterAnimation Animation to use when showing the fullscreen extract UI after it had previously been hidden. 
android:imeExtractExitAnimation Animation to use when hiding the fullscreen extract UI after it had previously been shown. 
android:imeFullscreenBackground Background to use for entire input method when it is being shown in fullscreen mode with the extract view, to ensure that it completely covers the application. 

Constants

int BACK_DISPOSITION_DEFAULT

The back button will close the input window.

int BACK_DISPOSITION_WILL_DISMISS

This input method will consume the back key.

int BACK_DISPOSITION_WILL_NOT_DISMISS

This input method will not consume the back key.

Inherited constants

From class android.app.Service
From class android.content.Context
From interface android.content.ComponentCallbacks2

Public constructors

InputMethodService()

Public methods

boolean enableHardwareAcceleration()

This method was deprecated in API level 21. Starting in API 21, hardware acceleration is always enabled on capable devices.

int getBackDisposition()
int getCandidatesHiddenVisibility()

Returns the visibility mode (either View.INVISIBLE or View.GONE) of the candidates view when it is not shown.

InputBinding getCurrentInputBinding()

Return the currently active InputBinding for the input method, or null if there is none.

InputConnection getCurrentInputConnection()

Retrieve the currently active InputConnection that is bound to the input method, or null if there is none.

EditorInfo getCurrentInputEditorInfo()
boolean getCurrentInputStarted()
int getInputMethodWindowRecommendedHeight()
LayoutInflater getLayoutInflater()
int getMaxWidth()

Return the maximum width, in pixels, available the input method.

CharSequence getTextForImeAction(int imeOptions)

Return text that can be used as a button label for the given EditorInfo.imeOptions.

Dialog getWindow()
void hideStatusIcon()
void hideWindow()
boolean isExtractViewShown()

Return whether the fullscreen extract view is shown.

boolean isFullscreenMode()

Return whether the input method is currently running in fullscreen mode.

boolean isInputViewShown()

Return whether the soft input view is currently shown to the user.

boolean isShowInputRequested()

Returns true if we have been asked to show our input view.

void onAppPrivateCommand(String action, Bundle data)
void onBindInput()

Called when a new client has bound to the input method.

void onComputeInsets(InputMethodService.Insets outInsets)

Compute the interesting insets into your UI.

void onConfigurationChanged(Configuration newConfig)

Take care of handling configuration changes.

void onConfigureWindow(Window win, boolean isFullscreen, boolean isCandidatesOnly)

Update the given window's parameters for the given mode.

void onCreate()

Called by the system when the service is first created.

View onCreateCandidatesView()

Create and return the view hierarchy used to show candidates.

View onCreateExtractTextView()

Called by the framework to create the layout for showing extacted text.

AbstractInputMethodService.AbstractInputMethodImpl onCreateInputMethodInterface()

Implement to return our standard InputMethodService.InputMethodImpl.

AbstractInputMethodService.AbstractInputMethodSessionImpl onCreateInputMethodSessionInterface()

Implement to return our standard InputMethodService.InputMethodSessionImpl.

View onCreateInputView()

Create and return the view hierarchy used for the input area (such as a soft keyboard).

void onDestroy()

Called by the system to notify a Service that it is no longer used and is being removed.

void onDisplayCompletions(CompletionInfo[] completions)

Called when the application has reported auto-completion candidates that it would like to have the input method displayed.

boolean onEvaluateFullscreenMode()

Override this to control when the input method should run in fullscreen mode.

boolean onEvaluateInputViewShown()

Override this to control when the soft input area should be shown to the user.

boolean onExtractTextContextMenuItem(int id)

This is called when the user has selected a context menu item from the extracted text view, when running in fullscreen mode.

void onExtractedCursorMovement(int dx, int dy)

This is called when the user has performed a cursor movement in the extracted text view, when it is running in fullscreen mode.

void onExtractedSelectionChanged(int start, int end)

This is called when the user has moved the cursor in the extracted text view, when running in fullsreen mode.

void onExtractedTextClicked()

This is called when the user has clicked on the extracted text view, when running in fullscreen mode.

void onExtractingInputChanged(EditorInfo ei)

This is called when, while currently displayed in extract mode, the current input target changes.

void onFinishCandidatesView(boolean finishingInput)

Called when the candidates view is being hidden from the user.

void onFinishInput()

Called to inform the input method that text input has finished in the last editor.

void onFinishInputView(boolean finishingInput)

Called when the input view is being hidden from the user.

boolean onGenericMotionEvent(MotionEvent event)

Override this to intercept generic motion events before they are processed by the application.

void onInitializeInterface()

This is a hook that subclasses can use to perform initialization of their interface.

boolean onKeyDown(int keyCode, KeyEvent event)

Override this to intercept key down events before they are processed by the application.

boolean onKeyLongPress(int keyCode, KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyLongPress(): always returns false (doesn't handle the event).

boolean onKeyMultiple(int keyCode, int count, KeyEvent event)

Override this to intercept special key multiple events before they are processed by the application.

boolean onKeyUp(int keyCode, KeyEvent event)

Override this to intercept key up events before they are processed by the application.

boolean onShowInputRequested(int flags, boolean configChange)

The system has decided that it may be time to show your input method.

void onStartCandidatesView(EditorInfo info, boolean restarting)

Called when only the candidates view has been shown for showing processing as the user enters text through a hard keyboard.

void onStartInput(EditorInfo attribute, boolean restarting)

Called to inform the input method that text input has started in an editor.

void onStartInputView(EditorInfo info, boolean restarting)

Called when the input view is being shown and input has started on a new editor.

boolean onTrackballEvent(MotionEvent event)

Override this to intercept trackball motion events before they are processed by the application.

void onUnbindInput()

Called when the previous bound client is no longer associated with the input method.

void onUpdateCursor(Rect newCursor)

This method was deprecated in API level 21. Use {#link onUpdateCursorAnchorInfo(CursorAnchorInfo)} instead.

void onUpdateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo)

Called when the application has reported a new location of its text insertion point and characters in the composition string.

void onUpdateExtractedText(int token, ExtractedText text)

Called when the application has reported new extracted text to be shown due to changes in its current text state.

void onUpdateExtractingViews(EditorInfo ei)

Called when the fullscreen-mode extracting editor info has changed, to update the state of its UI such as the action buttons shown.

void onUpdateExtractingVisibility(EditorInfo ei)

Called when the fullscreen-mode extracting editor info has changed, to determine whether the extracting (extract text and candidates) portion of the UI should be shown.

void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd)

Called when the application has reported a new selection region of the text.

void onViewClicked(boolean focusChanged)

Called when the user tapped or clicked a text view.

void onWindowHidden()

Called when the input method window has been hidden from the user, after previously being visible.

void onWindowShown()

Called when the input method window has been shown to the user, after previously not being visible.

void requestHideSelf(int flags)

Close this input method's soft input area, removing it from the display.

boolean sendDefaultEditorAction(boolean fromEnterKey)

Ask the input target to execute its default action via InputConnection.performEditorAction().

void sendDownUpKeyEvents(int keyEventCode)

Send the given key event code (as defined by KeyEvent) to the current input connection is a key down + key up event pair.

void sendKeyChar(char charCode)

Send the given UTF-16 character to the current input connection.

void setBackDisposition(int disposition)
void setCandidatesView(View view)

Replaces the current candidates view with a new one.

void setCandidatesViewShown(boolean shown)

Controls the visibility of the candidates display area.

void setExtractView(View view)
void setExtractViewShown(boolean shown)

Controls the visibility of the extracted text area.

void setInputView(View view)

Replaces the current input view with a new one.

void setTheme(int theme)

You can call this to customize the theme used by your IME's window.

void showStatusIcon(int iconResId)
void showWindow(boolean showInput)
void switchInputMethod(String id)

Force switch to a new input method, as identified by id.

void updateFullscreenMode()

Re-evaluate whether the input method should be running in fullscreen mode, and update its UI if this has changed since the last time it was evaluated.

void updateInputViewShown()

Re-evaluate whether the soft input area should currently be shown, and update its UI if this has changed since the last time it was evaluated.

Protected methods

void dump(FileDescriptor fd, PrintWriter fout, String[] args)

Performs a dump of the InputMethodService's internal state.

void onCurrentInputMethodSubtypeChanged(InputMethodSubtype newSubtype)

Called when the subtype was changed.

Inherited methods

From class android.inputmethodservice.AbstractInputMethodService
From class android.app.Service
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object
From interface android.view.KeyEvent.Callback
From interface android.content.ComponentCallbacks2
From interface android.content.ComponentCallbacks

XML attributes

android:imeExtractEnterAnimation

Animation to use when showing the fullscreen extract UI after it had previously been hidden.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol imeExtractEnterAnimation.

android:imeExtractExitAnimation

Animation to use when hiding the fullscreen extract UI after it had previously been shown.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol imeExtractExitAnimation.

android:imeFullscreenBackground

Background to use for entire input method when it is being shown in fullscreen mode with the extract view, to ensure that it completely covers the application. This allows, for example, the candidate view to be hidden while in fullscreen mode without having the application show through behind it.

May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This corresponds to the global attribute resource symbol imeFullscreenBackground.

Constants

BACK_DISPOSITION_DEFAULT

Added in API level 11
int BACK_DISPOSITION_DEFAULT

The back button will close the input window.

Constant Value: 0 (0x00000000)

BACK_DISPOSITION_WILL_DISMISS

Added in API level 11
int BACK_DISPOSITION_WILL_DISMISS

This input method will consume the back key.

Constant Value: 2 (0x00000002)

BACK_DISPOSITION_WILL_NOT_DISMISS

Added in API level 11
int BACK_DISPOSITION_WILL_NOT_DISMISS

This input method will not consume the back key.

Constant Value: 1 (0x00000001)

Public constructors

InputMethodService

Added in API level 3
InputMethodService ()

Public methods

enableHardwareAcceleration

Added in API level 17
boolean enableHardwareAcceleration ()

This method was deprecated in API level 21.
Starting in API 21, hardware acceleration is always enabled on capable devices.

You can call this to try to enable hardware accelerated drawing for your IME. This must be set before onCreate(), so you will typically call it in your constructor. It is not always possible to use hardware accelerated drawing in an IME (for example on low-end devices that do not have the resources to support this), so the call returns true if it succeeds otherwise false if you will need to draw in software. You must be able to handle either case.

Returns
boolean

getBackDisposition

Added in API level 11
int getBackDisposition ()

Returns
int

getCandidatesHiddenVisibility

Added in API level 3
int getCandidatesHiddenVisibility ()

Returns the visibility mode (either View.INVISIBLE or View.GONE) of the candidates view when it is not shown. The default implementation returns GONE when isExtractViewShown() returns true, otherwise VISIBLE. Be careful if you change this to return GONE in other situations -- if showing or hiding the candidates view causes your window to resize, this can cause temporary drawing artifacts as the resize takes place.

Returns
int

getCurrentInputBinding

Added in API level 3
InputBinding getCurrentInputBinding ()

Return the currently active InputBinding for the input method, or null if there is none.

Returns
InputBinding

getCurrentInputConnection

Added in API level 3
InputConnection getCurrentInputConnection ()

Retrieve the currently active InputConnection that is bound to the input method, or null if there is none.

Returns
InputConnection

getCurrentInputEditorInfo

Added in API level 3
EditorInfo getCurrentInputEditorInfo ()

Returns
EditorInfo

getCurrentInputStarted

Added in API level 3
boolean getCurrentInputStarted ()

Returns
boolean

getInputMethodWindowRecommendedHeight

Added in API level 21
int getInputMethodWindowRecommendedHeight ()

Returns
int The recommended height of the input method window. An IME author can get the last input method's height as the recommended height by calling this in onStartInputView(EditorInfo, boolean). If you don't need to use a predefined fixed height, you can avoid the window-resizing of IME switching by using this value as a visible inset height. It's efficient for the smooth transition between different IMEs. However, note that this may return 0 (or possibly unexpectedly low height). You should thus avoid relying on the return value of this method all the time. Please make sure to use a reasonable height for the IME.

getLayoutInflater

Added in API level 3
LayoutInflater getLayoutInflater ()

Returns
LayoutInflater

getMaxWidth

Added in API level 3
int getMaxWidth ()

Return the maximum width, in pixels, available the input method. Input methods are positioned at the bottom of the screen and, unless running in fullscreen, will generally want to be as short as possible so should compute their height based on their contents. However, they can stretch as much as needed horizontally. The function returns to you the maximum amount of space available horizontally, which you can use if needed for UI placement.

In many cases this is not needed, you can just rely on the normal view layout mechanisms to position your views within the full horizontal space given to the input method.

Note that this value can change dynamically, in particular when the screen orientation changes.

Returns
int

getTextForImeAction

Added in API level 3
CharSequence getTextForImeAction (int imeOptions)

Return text that can be used as a button label for the given EditorInfo.imeOptions. Returns null if there is no action requested. Note that there is no guarantee that the returned text will be relatively short, so you probably do not want to use it as text on a soft keyboard key label.

Parameters
imeOptions int: The value from @link EditorInfo#imeOptions EditorInfo.imeOptions}.
Returns
CharSequence Returns a label to use, or null if there is no action.

getWindow

Added in API level 3
Dialog getWindow ()

Returns
Dialog

hideStatusIcon

Added in API level 3
void hideStatusIcon ()

hideWindow

Added in API level 3
void hideWindow ()

isExtractViewShown

Added in API level 3
boolean isExtractViewShown ()

Return whether the fullscreen extract view is shown. This will only return true if isFullscreenMode() returns true, and in that case its value depends on the last call to setExtractViewShown(boolean). This effectively lets you determine if the application window is entirely covered (when this returns true) or if some part of it may be shown (if this returns false, though if isFullscreenMode() returns true in that case then it is probably only a sliver of the application).

Returns
boolean

isFullscreenMode

Added in API level 3
boolean isFullscreenMode ()

Return whether the input method is currently running in fullscreen mode. This is the mode that was last determined and applied by updateFullscreenMode().

Returns
boolean

isInputViewShown

Added in API level 3
boolean isInputViewShown ()

Return whether the soft input view is currently shown to the user. This is the state that was last determined and applied by updateInputViewShown().

Returns
boolean

isShowInputRequested

Added in API level 3
boolean isShowInputRequested ()

Returns true if we have been asked to show our input view.

Returns
boolean

onAppPrivateCommand

Added in API level 3
void onAppPrivateCommand (String action, 
                Bundle data)

Parameters
action String
data Bundle

onBindInput

Added in API level 3
void onBindInput ()

Called when a new client has bound to the input method. This may be followed by a series of onStartInput(EditorInfo, boolean) and onFinishInput() calls as the user navigates through its UI. Upon this call you know that getCurrentInputBinding() and getCurrentInputConnection() return valid objects.

onComputeInsets

Added in API level 3
void onComputeInsets (InputMethodService.Insets outInsets)

Compute the interesting insets into your UI. The default implementation uses the top of the candidates frame for the visible insets, and the top of the input frame for the content insets. The default touchable insets are TOUCHABLE_INSETS_VISIBLE.

Note that this method is not called when isExtractViewShown() returns true, since in that case the application is left as-is behind the input method and not impacted by anything in its UI.

Parameters
outInsets InputMethodService.Insets: Fill in with the current UI insets.

onConfigurationChanged

Added in API level 3
void onConfigurationChanged (Configuration newConfig)

Take care of handling configuration changes. Subclasses of InputMethodService generally don't need to deal directly with this on their own; the standard implementation here takes care of regenerating the input method UI as a result of the configuration change, so you can rely on your onCreateInputView() and other methods being called as appropriate due to a configuration change.

When a configuration change does happen, onInitializeInterface() is guaranteed to be called the next time prior to any of the other input or UI creation callbacks. The following will be called immediately depending if appropriate for current state: onStartInput(EditorInfo, boolean) if input is active, and onCreateInputView() and onStartInputView(EditorInfo, boolean) and related appropriate functions if the UI is displayed.

Parameters
newConfig Configuration: The new device configuration.

onConfigureWindow

Added in API level 3
void onConfigureWindow (Window win, 
                boolean isFullscreen, 
                boolean isCandidatesOnly)

Update the given window's parameters for the given mode. This is called when the window is first displayed and each time the fullscreen or candidates only mode changes.

The default implementation makes the layout for the window MATCH_PARENT x MATCH_PARENT when in fullscreen mode, and MATCH_PARENT x WRAP_CONTENT when in non-fullscreen mode.

Parameters
win Window: The input method's window.
isFullscreen boolean: If true, the window is running in fullscreen mode and intended to cover the entire application display.
isCandidatesOnly boolean: If true, the window is only showing the candidates view and none of the rest of its UI. This is mutually exclusive with fullscreen mode.

onCreate

Added in API level 3
void onCreate ()

Called by the system when the service is first created. Do not call this method directly.

onCreateCandidatesView

Added in API level 3
View onCreateCandidatesView ()

Create and return the view hierarchy used to show candidates. This will be called once, when the candidates are first displayed. You can return null to have no candidates view; the default implementation returns null.

To control when the candidates view is displayed, use setCandidatesViewShown(boolean). To change the candidates view after the first one is created by this function, use setCandidatesView(View).

Returns
View

onCreateExtractTextView

Added in API level 3
View onCreateExtractTextView ()

Called by the framework to create the layout for showing extacted text. Only called when in fullscreen mode. The returned view hierarchy must have an ExtractEditText whose ID is inputExtractEditText.

Returns
View

onCreateInputMethodInterface

Added in API level 3
AbstractInputMethodService.AbstractInputMethodImpl onCreateInputMethodInterface ()

Implement to return our standard InputMethodService.InputMethodImpl. Subclasses can override to provide their own customized version.

Returns
AbstractInputMethodService.AbstractInputMethodImpl

onCreateInputMethodSessionInterface

Added in API level 3
AbstractInputMethodService.AbstractInputMethodSessionImpl onCreateInputMethodSessionInterface ()

Implement to return our standard InputMethodService.InputMethodSessionImpl. Subclasses can override to provide their own customized version.

Returns
AbstractInputMethodService.AbstractInputMethodSessionImpl

onCreateInputView

Added in API level 3
View onCreateInputView ()

Create and return the view hierarchy used for the input area (such as a soft keyboard). This will be called once, when the input area is first displayed. You can return null to have no input area; the default implementation returns null.

To control when the input view is displayed, implement onEvaluateInputViewShown(). To change the input view after the first one is created by this function, use setInputView(View).

Returns
View

onDestroy

Added in API level 3
void onDestroy ()

Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.

onDisplayCompletions

Added in API level 3
void onDisplayCompletions (CompletionInfo[] completions)

Called when the application has reported auto-completion candidates that it would like to have the input method displayed. Typically these are only used when an input method is running in full-screen mode, since otherwise the user can see and interact with the pop-up window of completions shown by the application.

The default implementation here does nothing.

Parameters
completions CompletionInfo

onEvaluateFullscreenMode

Added in API level 3
boolean onEvaluateFullscreenMode ()

Override this to control when the input method should run in fullscreen mode. The default implementation runs in fullsceen only when the screen is in landscape mode. If you change what this returns, you will need to call updateFullscreenMode() yourself whenever the returned value may have changed to have it re-evaluated and applied.

Returns
boolean

onEvaluateInputViewShown

Added in API level 3
boolean onEvaluateInputViewShown ()

Override this to control when the soft input area should be shown to the user. The default implementation returns false when there is no hard keyboard or the keyboard is hidden unless the user shows an intention to use software keyboard. If you change what this returns, you will need to call updateInputViewShown() yourself whenever the returned value may have changed to have it re-evaluated and applied.

When you override this method, it is recommended to call super.onEvaluateInputViewShown() and return true when true is returned.

Returns
boolean

onExtractTextContextMenuItem

Added in API level 3
boolean onExtractTextContextMenuItem (int id)

This is called when the user has selected a context menu item from the extracted text view, when running in fullscreen mode. The default implementation sends this action to the current InputConnection's performContextMenuAction(int), for it to be processed in underlying "real" editor. Re-implement this to provide whatever behavior you want.

Parameters
id int
Returns
boolean

onExtractedCursorMovement

Added in API level 3
void onExtractedCursorMovement (int dx, 
                int dy)

This is called when the user has performed a cursor movement in the extracted text view, when it is running in fullscreen mode. The default implementation hides the candidates view when a vertical movement happens, but only if the extracted text editor has a vertical scroll bar because its text doesn't fit. Re-implement this to provide whatever behavior you want.

Parameters
dx int: The amount of cursor movement in the x dimension.
dy int: The amount of cursor movement in the y dimension.

onExtractedSelectionChanged

Added in API level 3
void onExtractedSelectionChanged (int start, 
                int end)

This is called when the user has moved the cursor in the extracted text view, when running in fullsreen mode. The default implementation performs the corresponding selection change on the underlying text editor.

Parameters
start int
end int

onExtractedTextClicked

Added in API level 3
void onExtractedTextClicked ()

This is called when the user has clicked on the extracted text view, when running in fullscreen mode. The default implementation hides the candidates view when this happens, but only if the extracted text editor has a vertical scroll bar because its text doesn't fit. Re-implement this to provide whatever behavior you want.

onExtractingInputChanged

Added in API level 3
void onExtractingInputChanged (EditorInfo ei)

This is called when, while currently displayed in extract mode, the current input target changes. The default implementation will auto-hide the IME if the new target is not a full editor, since this can be a confusing experience for the user.

Parameters
ei EditorInfo

onFinishCandidatesView

Added in API level 3
void onFinishCandidatesView (boolean finishingInput)

Called when the candidates view is being hidden from the user. This will be called either prior to hiding the window, or prior to switching to another target for editing.

The default implementation uses the InputConnection to clear any active composing text; you can override this (not calling the base class implementation) to perform whatever behavior you would like.

Parameters
finishingInput boolean: If true, onFinishInput() will be called immediately after.

onFinishInput

Added in API level 3
void onFinishInput ()

Called to inform the input method that text input has finished in the last editor. At this point there may be a call to onStartInput(EditorInfo, boolean) to perform input in a new editor, or the input method may be left idle. This method is not called when input restarts in the same editor.

The default implementation uses the InputConnection to clear any active composing text; you can override this (not calling the base class implementation) to perform whatever behavior you would like.

onFinishInputView

Added in API level 3
void onFinishInputView (boolean finishingInput)

Called when the input view is being hidden from the user. This will be called either prior to hiding the window, or prior to switching to another target for editing.

The default implementation uses the InputConnection to clear any active composing text; you can override this (not calling the base class implementation) to perform whatever behavior you would like.

Parameters
finishingInput boolean: If true, onFinishInput() will be called immediately after.

onGenericMotionEvent

Added in API level 17
boolean onGenericMotionEvent (MotionEvent event)

Override this to intercept generic motion events before they are processed by the application. If you return true, the application will not itself process the event. If you return false, the normal application processing will occur as if the IME had not seen the event at all.

Parameters
event MotionEvent: The motion event being received.
Returns
boolean True if the event was handled in this function, false otherwise.

onInitializeInterface

Added in API level 3
void onInitializeInterface ()

This is a hook that subclasses can use to perform initialization of their interface. It is called for you prior to any of your UI objects being created, both after the service is first created and after a configuration change happens.

onKeyDown

Added in API level 3
boolean onKeyDown (int keyCode, 
                KeyEvent event)

Override this to intercept key down events before they are processed by the application. If you return true, the application will not process the event itself. If you return false, the normal application processing will occur as if the IME had not seen the event at all.

The default implementation intercepts KeyEvent.KEYCODE_BACK if the IME is currently shown, to possibly hide it when the key goes up (if not canceled or long pressed). In addition, in fullscreen mode only, it will consume DPAD movement events to move the cursor in the extracted text view, not allowing them to perform navigation in the underlying application.

Parameters
keyCode int
event KeyEvent
Returns
boolean

onKeyLongPress

Added in API level 5
boolean onKeyLongPress (int keyCode, 
                KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyLongPress(): always returns false (doesn't handle the event).

Parameters
keyCode int
event KeyEvent
Returns
boolean

onKeyMultiple

Added in API level 3
boolean onKeyMultiple (int keyCode, 
                int count, 
                KeyEvent event)

Override this to intercept special key multiple events before they are processed by the application. If you return true, the application will not itself process the event. If you return false, the normal application processing will occur as if the IME had not seen the event at all.

The default implementation always returns false, except when in fullscreen mode, where it will consume DPAD movement events to move the cursor in the extracted text view, not allowing them to perform navigation in the underlying application.

Parameters
keyCode int
count int
event KeyEvent
Returns
boolean

onKeyUp

Added in API level 3
boolean onKeyUp (int keyCode, 
                KeyEvent event)

Override this to intercept key up events before they are processed by the application. If you return true, the application will not itself process the event. If you return false, the normal application processing will occur as if the IME had not seen the event at all.

The default implementation intercepts KeyEvent.KEYCODE_BACK to hide the current IME UI if it is shown. In addition, in fullscreen mode only, it will consume DPAD movement events to move the cursor in the extracted text view, not allowing them to perform navigation in the underlying application.

Parameters
keyCode int
event KeyEvent
Returns
boolean

onShowInputRequested

Added in API level 3
boolean onShowInputRequested (int flags, 
                boolean configChange)

The system has decided that it may be time to show your input method. This is called due to a corresponding call to your InputMethod.showSoftInput() method. The default implementation uses onEvaluateInputViewShown(), onEvaluateFullscreenMode(), and the current configuration to decide whether the input view should be shown at this point.

Parameters
flags int: Provides additional information about the show request, as per InputMethod.showSoftInput().
configChange boolean: This is true if we are re-showing due to a configuration change.
Returns
boolean Returns true to indicate that the window should be shown.

onStartCandidatesView

Added in API level 3
void onStartCandidatesView (EditorInfo info, 
                boolean restarting)

Called when only the candidates view has been shown for showing processing as the user enters text through a hard keyboard. This will always be called after onStartInput(EditorInfo, boolean), allowing you to do your general setup there and just view-specific setup here. You are guaranteed that onCreateCandidatesView() will have been called some time before this function is called.

Note that this will not be called when the input method is running in full editing mode, and thus receiving onStartInputView(EditorInfo, boolean) to initiate that operation. This is only for the case when candidates are being shown while the input method editor is hidden but wants to show its candidates UI as text is entered through some other mechanism.

Parameters
info EditorInfo: Description of the type of text being edited.
restarting boolean: Set to true if we are restarting input on the same text field as before.

onStartInput

Added in API level 3
void onStartInput (EditorInfo attribute, 
                boolean restarting)

Called to inform the input method that text input has started in an editor. You should use this callback to initialize the state of your input to match the state of the editor given to it.

Parameters
attribute EditorInfo: The attributes of the editor that input is starting in.
restarting boolean: Set to true if input is restarting in the same editor such as because the application has changed the text in the editor. Otherwise will be false, indicating this is a new session with the editor.

onStartInputView

Added in API level 3
void onStartInputView (EditorInfo info, 
                boolean restarting)

Called when the input view is being shown and input has started on a new editor. This will always be called after onStartInput(EditorInfo, boolean), allowing you to do your general setup there and just view-specific setup here. You are guaranteed that onCreateInputView() will have been called some time before this function is called.

Parameters
info EditorInfo: Description of the type of text being edited.
restarting boolean: Set to true if we are restarting input on the same text field as before.

onTrackballEvent

Added in API level 3
boolean onTrackballEvent (MotionEvent event)

Override this to intercept trackball motion events before they are processed by the application. If you return true, the application will not itself process the event. If you return false, the normal application processing will occur as if the IME had not seen the event at all.

Parameters
event MotionEvent: The motion event being received.
Returns
boolean True if the event was handled in this function, false otherwise.

onUnbindInput

Added in API level 3
void onUnbindInput ()

Called when the previous bound client is no longer associated with the input method. After returning getCurrentInputBinding() and getCurrentInputConnection() will no longer return valid objects.

onUpdateCursor

Added in API level 3
void onUpdateCursor (Rect newCursor)

This method was deprecated in API level 21.
Use {#link onUpdateCursorAnchorInfo(CursorAnchorInfo)} instead.

Called when the application has reported a new location of its text cursor. This is only called if explicitly requested by the input method. The default implementation does nothing.

Parameters
newCursor Rect

onUpdateCursorAnchorInfo

Added in API level 21
void onUpdateCursorAnchorInfo (CursorAnchorInfo cursorAnchorInfo)

Called when the application has reported a new location of its text insertion point and characters in the composition string. This is only called if explicitly requested by the input method. The default implementation does nothing.

Parameters
cursorAnchorInfo CursorAnchorInfo: The positional information of the text insertion point and the composition string.

onUpdateExtractedText

Added in API level 3
void onUpdateExtractedText (int token, 
                ExtractedText text)

Called when the application has reported new extracted text to be shown due to changes in its current text state. The default implementation here places the new text in the extract edit text, when the input method is running in fullscreen mode.

Parameters
token int
text ExtractedText

onUpdateExtractingViews

Added in API level 3
void onUpdateExtractingViews (EditorInfo ei)

Called when the fullscreen-mode extracting editor info has changed, to update the state of its UI such as the action buttons shown. You do not need to deal with this if you are using the standard full screen extract UI. If replacing it, you will need to re-implement this to put the appropriate action button in your own UI and handle it, and perform any other changes.

The standard implementation turns on or off its accessory area depending on whether there is an action button, and hides or shows the entire extract area depending on whether it makes sense for the current editor. In particular, a TYPE_NULL or TYPE_TEXT_VARIATION_FILTER input type will turn off the extract area since there is no text to be shown.

Parameters
ei EditorInfo

onUpdateExtractingVisibility

Added in API level 3
void onUpdateExtractingVisibility (EditorInfo ei)

Called when the fullscreen-mode extracting editor info has changed, to determine whether the extracting (extract text and candidates) portion of the UI should be shown. The standard implementation hides or shows the extract area depending on whether it makes sense for the current editor. In particular, a TYPE_NULL input type or IME_FLAG_NO_EXTRACT_UI flag will turn off the extract area since there is no text to be shown.

Parameters
ei EditorInfo

onUpdateSelection

Added in API level 3
void onUpdateSelection (int oldSelStart, 
                int oldSelEnd, 
                int newSelStart, 
                int newSelEnd, 
                int candidatesStart, 
                int candidatesEnd)

Called when the application has reported a new selection region of the text. This is called whether or not the input method has requested extracted text updates, although if so it will not receive this call if the extracted text has changed as well.

Be careful about changing the text in reaction to this call with methods such as setComposingText, commitText or deleteSurroundingText. If the cursor moves as a result, this method will be called again, which may result in an infinite loop.

The default implementation takes care of updating the cursor in the extract text, if it is being shown.

Parameters
oldSelStart int
oldSelEnd int
newSelStart int
newSelEnd int
candidatesStart int
candidatesEnd int

onViewClicked

Added in API level 14
void onViewClicked (boolean focusChanged)

Called when the user tapped or clicked a text view. IMEs can't rely on this method being called because this was not part of the original IME protocol, so applications with custom text editing written before this method appeared will not call to inform the IME of this interaction.

Parameters
focusChanged boolean: true if the user changed the focused view by this click.

onWindowHidden

Added in API level 3
void onWindowHidden ()

Called when the input method window has been hidden from the user, after previously being visible.

onWindowShown

Added in API level 3
void onWindowShown ()

Called when the input method window has been shown to the user, after previously not being visible. This is done after all of the UI setup for the window has occurred (creating its views etc).

requestHideSelf

Added in API level 3
void requestHideSelf (int flags)

Close this input method's soft input area, removing it from the display. The input method will continue running, but the user can no longer use it to generate input by touching the screen.

Parameters
flags int: Provides additional operating flags. Currently may be 0 or have the InputMethodManager.HIDE_IMPLICIT_ONLY bit set.

sendDefaultEditorAction

Added in API level 3
boolean sendDefaultEditorAction (boolean fromEnterKey)

Ask the input target to execute its default action via InputConnection.performEditorAction().

Parameters
fromEnterKey boolean: If true, this will be executed as if the user had pressed an enter key on the keyboard, that is it will not be done if the editor has set EditorInfo.IME_FLAG_NO_ENTER_ACTION. If false, the action will be sent regardless of how the editor has set that flag.
Returns
boolean Returns a boolean indicating whether an action has been sent. If false, either the editor did not specify a default action or it does not want an action from the enter key. If true, the action was sent (or there was no input connection at all).

sendDownUpKeyEvents

Added in API level 3
void sendDownUpKeyEvents (int keyEventCode)

Send the given key event code (as defined by KeyEvent) to the current input connection is a key down + key up event pair. The sent events have KeyEvent.FLAG_SOFT_KEYBOARD set, so that the recipient can identify them as coming from a software input method, and KeyEvent.FLAG_KEEP_TOUCH_MODE, so that they don't impact the current touch mode of the UI.

Note that it's discouraged to send such key events in normal operation; this is mainly for use with TYPE_NULL type text fields, or for non-rich input methods. A reasonably capable software input method should use the commitText(CharSequence, int) family of methods to send text to an application, rather than sending key events.

Parameters
keyEventCode int: The raw key code to send, as defined by KeyEvent.

sendKeyChar

Added in API level 3
void sendKeyChar (char charCode)

Send the given UTF-16 character to the current input connection. Most characters will be delivered simply by calling InputConnection.commitText() with the character; some, however, may be handled different. In particular, the enter character ('\n') will either be delivered as an action code or a raw key event, as appropriate. Consider this as a convenience method for IMEs that do not have a full implementation of actions; a fully complying IME will decide of the right action for each event and will likely never call this method except maybe to handle events coming from an actual hardware keyboard.

Parameters
charCode char: The UTF-16 character code to send.

setBackDisposition

Added in API level 11
void setBackDisposition (int disposition)

Parameters
disposition int

setCandidatesView

Added in API level 3
void setCandidatesView (View view)

Replaces the current candidates view with a new one. You only need to call this when dynamically changing the view; normally, you should implement onCreateCandidatesView() and create your view when first needed by the input method.

Parameters
view View

setCandidatesViewShown

Added in API level 3
void setCandidatesViewShown (boolean shown)

Controls the visibility of the candidates display area. By default it is hidden.

Parameters
shown boolean

setExtractView

Added in API level 3
void setExtractView (View view)

Parameters
view View

setExtractViewShown

Added in API level 3
void setExtractViewShown (boolean shown)

Controls the visibility of the extracted text area. This only applies when the input method is in fullscreen mode, and thus showing extracted text. When false, the extracted text will not be shown, allowing some of the application to be seen behind. This is normally set for you by onUpdateExtractingVisibility(EditorInfo). This controls the visibility of both the extracted text and candidate view; the latter since it is not useful if there is no text to see.

Parameters
shown boolean

setInputView

Added in API level 3
void setInputView (View view)

Replaces the current input view with a new one. You only need to call this when dynamically changing the view; normally, you should implement onCreateInputView() and create your view when first needed by the input method.

Parameters
view View

setTheme

Added in API level 3
void setTheme (int theme)

You can call this to customize the theme used by your IME's window. This theme should typically be one that derives from Theme_InputMethod, which is the default theme you will get. This must be set before onCreate(), so you will typically call it in your constructor with the resource ID of your custom theme.

Parameters
theme int: The style resource describing the theme.

showStatusIcon

Added in API level 3
void showStatusIcon (int iconResId)

Parameters
iconResId int

showWindow

Added in API level 3
void showWindow (boolean showInput)

Parameters
showInput boolean

switchInputMethod

Added in API level 3
void switchInputMethod (String id)

Force switch to a new input method, as identified by id. This input method will be destroyed, and the requested one started on the current input field.

Parameters
id String: Unique identifier of the new input method ot start.

updateFullscreenMode

Added in API level 3
void updateFullscreenMode ()

Re-evaluate whether the input method should be running in fullscreen mode, and update its UI if this has changed since the last time it was evaluated. This will call onEvaluateFullscreenMode() to determine whether it should currently run in fullscreen mode. You can use isFullscreenMode() to determine if the input method is currently running in fullscreen mode.

updateInputViewShown

Added in API level 3
void updateInputViewShown ()

Re-evaluate whether the soft input area should currently be shown, and update its UI if this has changed since the last time it was evaluated. This will call onEvaluateInputViewShown() to determine whether the input view should currently be shown. You can use isInputViewShown() to determine if the input view is currently shown.

Protected methods

dump

Added in API level 3
void dump (FileDescriptor fd, 
                PrintWriter fout, 
                String[] args)

Performs a dump of the InputMethodService's internal state. Override to add your own information to the dump.

Parameters
fd FileDescriptor: The raw file descriptor that the dump is being sent to.
fout PrintWriter: The PrintWriter to which you should dump your state. This will be closed for you after you return.
args String: additional arguments to the dump request.

onCurrentInputMethodSubtypeChanged

Added in API level 11
void onCurrentInputMethodSubtypeChanged (InputMethodSubtype newSubtype)

Called when the subtype was changed.

Parameters
newSubtype InputMethodSubtype: the subtype which is being changed to.

Hooray!