Skip to content

Most visited

Recently visited

navigation
Added in API level 3

KeyboardView

public class KeyboardView
extends View implements View.OnClickListener

java.lang.Object
   ↳ android.view.View
     ↳ android.inputmethodservice.KeyboardView


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

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. 

Inherited XML attributes

From class android.view.View

Inherited constants

From class android.view.View

Inherited fields

From class android.view.View

Public constructors

KeyboardView(Context context, AttributeSet attrs)
KeyboardView(Context context, AttributeSet attrs, int defStyleAttr)
KeyboardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

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()

Returns the KeyboardView.OnKeyboardActionListener object.

boolean onLongPress(Keyboard.Key popupKey)

Called when a key is long pressed.

void swipeDown()
void swipeLeft()
void swipeRight()
void swipeUp()

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.accessibility.AccessibilityEventSource
From interface android.view.View.OnClickListener

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.

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.

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.

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.

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.

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.

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.

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.

Public constructors

KeyboardView

Added in API level 3
KeyboardView (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

KeyboardView

Added in API level 3
KeyboardView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context
attrs AttributeSet
defStyleAttr int

KeyboardView

Added in API level 21
KeyboardView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context
attrs AttributeSet
defStyleAttr int
defStyleRes int

Public methods

closing

Added in API level 3
void closing ()

getKeyboard

Added in API level 3
Keyboard getKeyboard ()

Returns the current keyboard being displayed by this view.

Returns
Keyboard the currently attached keyboard

See also:

handleBack

Added in API level 3
boolean handleBack ()

Returns
boolean

invalidateAllKeys

Added in API level 4
void invalidateAllKeys ()

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.

See also:

invalidateKey

Added in API level 4
void invalidateKey (int keyIndex)

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 int: the index of the key in the attached Keyboard.

See also:

isPreviewEnabled

Added in API level 3
boolean isPreviewEnabled ()

Returns the enabled state of the key feedback popup.

Returns
boolean whether or not the key feedback popup is enabled

See also:

isProximityCorrectionEnabled

Added in API level 3
boolean isProximityCorrectionEnabled ()

Returns true if proximity correction is enabled.

Returns
boolean

isShifted

Added in API level 3
boolean isShifted ()

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

Returns
boolean 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.

See also:

onDetachedFromWindow

Added in API level 3
void onDetachedFromWindow ()

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

onDraw

Added in API level 3
void onDraw (Canvas canvas)

Implement this to do your drawing.

Parameters
canvas Canvas: the canvas on which the background will be drawn

onHoverEvent

Added in API level 14
boolean onHoverEvent (MotionEvent event)

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 MotionEvent: The motion event that describes the hover.
Returns
boolean True if the view handled the hover event.

onMeasure

Added in API level 3
void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

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 overridden 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 int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

onSizeChanged

Added in API level 3
void onSizeChanged (int w, 
                int h, 
                int oldw, 
                int oldh)

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 int: Current width of this view.
h int: Current height of this view.
oldw int: Old width of this view.
oldh int: Old height of this view.

onTouchEvent

Added in API level 3
boolean onTouchEvent (MotionEvent me)

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 MotionEvent: The motion event.
Returns
boolean True if the event was handled, false otherwise.

setKeyboard

Added in API level 3
void setKeyboard (Keyboard keyboard)

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 Keyboard: the keyboard to display in this view

See also:

setOnKeyboardActionListener

Added in API level 3
void setOnKeyboardActionListener (KeyboardView.OnKeyboardActionListener listener)

Parameters
listener KeyboardView.OnKeyboardActionListener

setPopupOffset

Added in API level 3
void setPopupOffset (int x, 
                int y)

Parameters
x int
y int

setPopupParent

Added in API level 3
void setPopupParent (View v)

Parameters
v View

setPreviewEnabled

Added in API level 3
void setPreviewEnabled (boolean previewEnabled)

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 boolean: whether or not to enable the key feedback popup

See also:

setProximityCorrectionEnabled

Added in API level 3
void setProximityCorrectionEnabled (boolean enabled)

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 boolean: whether or not the proximity correction is enabled

setShifted

Added in API level 3
boolean setShifted (boolean shifted)

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

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

See also:

setVerticalCorrection

Added in API level 3
void setVerticalCorrection (int verticalOffset)

Parameters
verticalOffset int

Protected methods

getOnKeyboardActionListener

Added in API level 3
KeyboardView.OnKeyboardActionListener getOnKeyboardActionListener ()

Returns the KeyboardView.OnKeyboardActionListener object.

Returns
KeyboardView.OnKeyboardActionListener the listener attached to this keyboard

onLongPress

Added in API level 3
boolean onLongPress (Keyboard.Key popupKey)

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 Keyboard.Key: the key that was long pressed
Returns
boolean 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.

swipeDown

Added in API level 3
void swipeDown ()

swipeLeft

Added in API level 3
void swipeLeft ()

swipeRight

Added in API level 3
void swipeRight ()

swipeUp

Added in API level 3
void swipeUp ()

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.