Android APIs
public class

KeyboardView

extends View
implements View.OnClickListener
java.lang.Object
   ↳ android.view.View
     ↳ android.inputmethodservice.KeyboardView

Class Overview

A view that renders a virtual Keyboard. It handles rendering of keys and detecting key presses and touch movements.

Summary

Nested Classes
interface KeyboardView.OnKeyboardActionListener Listener for virtual keyboard events. 
XML Attributes
Attribute Name Related Method Description
android:keyBackground Image for the key. 
android:keyPreviewLayout Layout resource for key press feedback. 
android:keyPreviewOffset Vertical offset of the key press feedback from the key. 
android:keyTextColor Color to use for the label in a key. 
android:keyTextSize Size of the text for character keys. 
android:labelTextSize Size of the text for custom keys with some text and no icon. 
android:popupLayout Layout resource for popup keyboards. 
android:verticalCorrection Amount to offset the touch Y coordinate by, for bias correction. 
[Expand]
Inherited XML Attributes
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
KeyboardView(Context context, AttributeSet attrs)
KeyboardView(Context context, AttributeSet attrs, int defStyle)
Public Methods
void closing()
Keyboard getKeyboard()
Returns the current keyboard being displayed by this view.
boolean handleBack()
void invalidateAllKeys()
Requests a redraw of the entire keyboard.
void invalidateKey(int keyIndex)
Invalidates a key so that it will be redrawn on the next repaint.
boolean isPreviewEnabled()
Returns the enabled state of the key feedback popup.
boolean isProximityCorrectionEnabled()
Returns true if proximity correction is enabled.
boolean isShifted()
Returns the state of the shift key of the keyboard, if any.
void onDetachedFromWindow()
This is called when the view is detached from a window.
void onDraw(Canvas canvas)
Implement this to do your drawing.
boolean onHoverEvent(MotionEvent event)
Implement this method to handle hover events.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

void onSizeChanged(int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed.
boolean onTouchEvent(MotionEvent me)
Implement this method to handle touch screen motion events.
void setKeyboard(Keyboard keyboard)
Attaches a keyboard to this view.
void setOnKeyboardActionListener(KeyboardView.OnKeyboardActionListener listener)
void setPopupOffset(int x, int y)
void setPopupParent(View v)
void setPreviewEnabled(boolean previewEnabled)
Enables or disables the key feedback popup.
void setProximityCorrectionEnabled(boolean enabled)
When enabled, calls to onKey(int, int[]) will include key codes for adjacent keys.
boolean setShifted(boolean shifted)
Sets the state of the shift key of the keyboard, if any.
void setVerticalCorrection(int verticalOffset)
Protected Methods
KeyboardView.OnKeyboardActionListener getOnKeyboardActionListener()
boolean onLongPress(Keyboard.Key popupKey)
Called when a key is long pressed.
void swipeDown()
void swipeLeft()
void swipeRight()
void swipeUp()
[Expand]
Inherited Methods
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.View.OnClickListener
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:keyBackground

Image for the key. This image needs to be a StateListDrawable, with the following possible states: normal, pressed, checkable, checkable+pressed, checkable+checked, checkable+checked+pressed.

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 keyBackground.

Related Methods

android:keyPreviewLayout

Layout resource for key press feedback.

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 keyPreviewLayout.

Related Methods

android:keyPreviewOffset

Vertical offset of the key press feedback from the key.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol keyPreviewOffset.

Related Methods

android:keyTextColor

Color to use for the label in a key.

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol keyTextColor.

Related Methods

android:keyTextSize

Size of the text for character keys.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol keyTextSize.

Related Methods

android:labelTextSize

Size of the text for custom keys with some text and no icon.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol labelTextSize.

Related Methods

android:popupLayout

Layout resource for popup keyboards.

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 popupLayout.

Related Methods

android:verticalCorrection

Amount to offset the touch Y coordinate by, for bias correction.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol verticalCorrection.

Related Methods

Public Constructors

public KeyboardView (Context context, AttributeSet attrs)

Added in API level 3

public KeyboardView (Context context, AttributeSet attrs, int defStyle)

Added in API level 3

Public Methods

public void closing ()

Added in API level 3

public Keyboard getKeyboard ()

Added in API level 3

Returns the current keyboard being displayed by this view.

Returns
  • the currently attached keyboard

public boolean handleBack ()

Added in API level 3

public void invalidateAllKeys ()

Added in API level 4

Requests a redraw of the entire keyboard. Calling invalidate() is not sufficient because the keyboard renders the keys to an off-screen buffer and an invalidate() only draws the cached buffer.

public void invalidateKey (int keyIndex)

Added in API level 4

Invalidates a key so that it will be redrawn on the next repaint. Use this method if only one key is changing it's content. Any changes that affect the position or size of the key may not be honored.

Parameters
keyIndex the index of the key in the attached Keyboard.

public boolean isPreviewEnabled ()

Added in API level 3

Returns the enabled state of the key feedback popup.

Returns
  • whether or not the key feedback popup is enabled

public boolean isProximityCorrectionEnabled ()

Added in API level 3

Returns true if proximity correction is enabled.

public boolean isShifted ()

Added in API level 3

Returns the state of the shift key of the keyboard, if any.

Returns
  • true if the shift is in a pressed state, false otherwise. If there is no shift key on the keyboard or there is no keyboard attached, it returns false.

public void onDetachedFromWindow ()

Added in API level 3

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

public void onDraw (Canvas canvas)

Added in API level 3

Implement this to do your drawing.

Parameters
canvas the canvas on which the background will be drawn

public boolean onHoverEvent (MotionEvent event)

Added in API level 14

Implement this method to handle hover events.

This method is called whenever a pointer is hovering into, over, or out of the bounds of a view and the view is not currently being touched. Hover events are represented as pointer events with action ACTION_HOVER_ENTER, ACTION_HOVER_MOVE, or ACTION_HOVER_EXIT.

  • The view receives a hover event with action ACTION_HOVER_ENTER when the pointer enters the bounds of the view.
  • The view receives a hover event with action ACTION_HOVER_MOVE when the pointer has already entered the bounds of the view and has moved.
  • The view receives a hover event with action ACTION_HOVER_EXIT when the pointer has exited the bounds of the view or when the pointer is about to go down due to a button click, tap, or similar user action that causes the view to be touched.

The view should implement this method to return true to indicate that it is handling the hover event, such as by changing its drawable state.

The default implementation calls setHovered(boolean) to update the hovered state of the view when a hover enter or hover exit event is received, if the view is enabled and is clickable. The default implementation also sends hover accessibility events.

Parameters
event The motion event that describes the hover.
Returns
  • True if the view handled the hover event.

public void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Added in API level 3

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

public void onSizeChanged (int w, int h, int oldw, int oldh)

Added in API level 3

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Parameters
w Current width of this view.
h Current height of this view.
oldw Old width of this view.
oldh Old height of this view.

public boolean onTouchEvent (MotionEvent me)

Added in API level 3

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
me The motion event.
Returns
  • True if the event was handled, false otherwise.

public void setKeyboard (Keyboard keyboard)

Added in API level 3

Attaches a keyboard to this view. The keyboard can be switched at any time and the view will re-layout itself to accommodate the keyboard.

Parameters
keyboard the keyboard to display in this view

public void setOnKeyboardActionListener (KeyboardView.OnKeyboardActionListener listener)

Added in API level 3

public void setPopupOffset (int x, int y)

Added in API level 3

public void setPopupParent (View v)

Added in API level 3

public void setPreviewEnabled (boolean previewEnabled)

Added in API level 3

Enables or disables the key feedback popup. This is a popup that shows a magnified version of the depressed key. By default the preview is enabled.

Parameters
previewEnabled whether or not to enable the key feedback popup

public void setProximityCorrectionEnabled (boolean enabled)

Added in API level 3

When enabled, calls to onKey(int, int[]) will include key codes for adjacent keys. When disabled, only the primary key code will be reported.

Parameters
enabled whether or not the proximity correction is enabled

public boolean setShifted (boolean shifted)

Added in API level 3

Sets the state of the shift key of the keyboard, if any.

Parameters
shifted whether or not to enable the state of the shift key
Returns
  • true if the shift key state changed, false if there was no change
See Also

public void setVerticalCorrection (int verticalOffset)

Added in API level 3

Protected Methods

protected KeyboardView.OnKeyboardActionListener getOnKeyboardActionListener ()

Added in API level 3
Returns
  • the listener attached to this keyboard

protected boolean onLongPress (Keyboard.Key popupKey)

Added in API level 3

Called when a key is long pressed. By default this will open any popup keyboard associated with this key through the attributes popupLayout and popupCharacters.

Parameters
popupKey the key that was long pressed
Returns
  • true if the long press is handled, false otherwise. Subclasses should call the method on the base class if the subclass doesn't wish to handle the call.

protected void swipeDown ()

Added in API level 3

protected void swipeLeft ()

Added in API level 3

protected void swipeRight ()

Added in API level 3

protected void swipeUp ()

Added in API level 3