Added in API level 3
Deprecated in API level 29

KeyboardView


open class KeyboardView : View, View.OnClickListener
kotlin.Any
   ↳ 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
abstract

Listener for virtual keyboard events.

XML attributes
android:keyBackground Image for the key.
android:keyPreviewHeight Height of the key press feedback popup.
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
Inherited constants
Public constructors
KeyboardView(context: Context!, attrs: AttributeSet!)

KeyboardView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

KeyboardView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open Unit

open Keyboard!

Returns the current keyboard being displayed by this view.

open Boolean

open Unit

Requests a redraw of the entire keyboard.

open Unit
invalidateKey(keyIndex: Int)

Invalidates a key so that it will be redrawn on the next repaint.

open Boolean

Returns the enabled state of the key feedback popup.

open Boolean

Returns true if proximity correction is enabled.

open Boolean

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

open Unit

Popup keyboard close button clicked.

open Unit

This is called when the view is detached from a window.

open Unit
onDraw(canvas: Canvas)

Implement this to do your drawing.

open Boolean

Implement this method to handle hover events.

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

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

open Unit
onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)

This is called during layout when the size of this view has changed.

open Boolean

Implement this method to handle pointer events.

open Unit
setKeyboard(keyboard: Keyboard!)

Attaches a keyboard to this view.

open Unit

open Unit

open Unit

open Unit
setPreviewEnabled(previewEnabled: Boolean)

Enables or disables the key feedback popup.

open Unit

When enabled, calls to OnKeyboardActionListener.onKey will include key codes for adjacent keys.

open Boolean
setShifted(shifted: Boolean)

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

open Unit
setVerticalCorrection(verticalOffset: Int)

Protected methods
open KeyboardView.OnKeyboardActionListener!

Returns the OnKeyboardActionListener object.

open Unit

This is called when the view is attached to a window.

open Boolean

Called when a key is long pressed.

open Unit

open Unit

open Unit

open Unit

Inherited functions
Inherited properties

XML attributes

android:keyBackground

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.

android:keyPreviewHeight

android:keyPreviewHeight
Height of the key press feedback popup.

android:keyPreviewLayout

android:keyPreviewLayout
Layout resource for key press feedback.

android:keyPreviewOffset

android:keyPreviewOffset
Vertical offset of the key press feedback from the key.

android:keyTextColor

android:keyTextColor
Color to use for the label in a key.

android:keyTextSize

android:keyTextSize
Size of the text for character keys.

android:labelTextSize

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

android:popupLayout

android:popupLayout
Layout resource for popup keyboards.

android:verticalCorrection

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

Public constructors

KeyboardView

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

KeyboardView

Added in API level 3
KeyboardView(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

KeyboardView

Added in API level 21
KeyboardView(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

closing

Added in API level 3
open fun closing(): Unit

Deprecated: Deprecated in Java.

getKeyboard

Added in API level 3
open fun getKeyboard(): Keyboard!

Deprecated: Deprecated in Java.

Returns the current keyboard being displayed by this view.

Return
Keyboard! the currently attached keyboard

handleBack

Added in API level 3
open fun handleBack(): Boolean

Deprecated: Deprecated in Java.

invalidateAllKeys

Added in API level 4
Deprecated in API level 29
open fun invalidateAllKeys(): Unit

Deprecated: Deprecated in Java.

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.

invalidateKey

Added in API level 4
Deprecated in API level 29
open fun invalidateKey(keyIndex: Int): Unit

Deprecated: Deprecated in Java.

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.

isPreviewEnabled

Added in API level 3
open fun isPreviewEnabled(): Boolean

Deprecated: Deprecated in Java.

Returns the enabled state of the key feedback popup.

Return
Boolean whether or not the key feedback popup is enabled

isProximityCorrectionEnabled

Added in API level 3
open fun isProximityCorrectionEnabled(): Boolean

Deprecated: Deprecated in Java.

Returns true if proximity correction is enabled.

isShifted

Added in API level 3
open fun isShifted(): Boolean

Deprecated: Deprecated in Java.

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

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

onClick

Added in API level 3
open fun onClick(v: View!): Unit

Deprecated: Deprecated in Java.

Popup keyboard close button clicked.

Parameters
v View!: The view that was clicked.

onDetachedFromWindow

Added in API level 3
open fun onDetachedFromWindow(): Unit

Deprecated: Deprecated in Java.

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
If you override this method you must call through to the superclass implementation.

onDraw

Added in API level 3
open fun onDraw(canvas: Canvas): Unit

Deprecated: Deprecated in Java.

Implement this to do your drawing.

Parameters
canvas Canvas: the canvas on which the background will be drawn.
This value cannot be null.

onHoverEvent

Added in API level 14
Deprecated in API level 29
open fun onHoverEvent(event: MotionEvent!): Boolean

Deprecated: Deprecated in Java.

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 MotionEvent.ACTION_HOVER_ENTER, MotionEvent.ACTION_HOVER_MOVE, or MotionEvent.ACTION_HOVER_EXIT.

  • The view receives a hover event with action MotionEvent.ACTION_HOVER_ENTER when the pointer enters the bounds of the view.
  • The view receives a hover event with action MotionEvent.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 MotionEvent.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 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.
Return
Boolean True if the view handled the hover event.

onMeasure

Added in API level 3
open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit

Deprecated: Deprecated in Java.

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

onSizeChanged

Added in API level 3
open fun onSizeChanged(
    w: Int,
    h: Int,
    oldw: Int,
    oldh: Int
): Unit

Deprecated: Deprecated in Java.

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
open fun onTouchEvent(me: MotionEvent!): Boolean

Deprecated: Deprecated in Java.

Implement this method to handle pointer events.

This method is called to handle motion events where pointers are down on the view. For example, this could include touchscreen touches, stylus touches, or click-and-drag events from a mouse. However, it is not called for motion events that do not involve pointers being down, such as hover events or mouse scroll wheel movements.

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
event The motion event.
Return
Boolean True if the event was handled, false otherwise.

setKeyboard

Added in API level 3
open fun setKeyboard(keyboard: Keyboard!): Unit

Deprecated: Deprecated in Java.

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

setOnKeyboardActionListener

Added in API level 3
open fun setOnKeyboardActionListener(listener: KeyboardView.OnKeyboardActionListener!): Unit

Deprecated: Deprecated in Java.

setPopupOffset

Added in API level 3
open fun setPopupOffset(
    x: Int,
    y: Int
): Unit

Deprecated: Deprecated in Java.

setPopupParent

Added in API level 3
open fun setPopupParent(v: View!): Unit

Deprecated: Deprecated in Java.

setPreviewEnabled

Added in API level 3
open fun setPreviewEnabled(previewEnabled: Boolean): Unit

Deprecated: Deprecated in Java.

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

setProximityCorrectionEnabled

Added in API level 3
open fun setProximityCorrectionEnabled(enabled: Boolean): Unit

Deprecated: Deprecated in Java.

When enabled, calls to OnKeyboardActionListener.onKey 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
open fun setShifted(shifted: Boolean): Boolean

Deprecated: Deprecated in Java.

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
Return
Boolean true if the shift key state changed, false if there was no change

setVerticalCorrection

Added in API level 3
open fun setVerticalCorrection(verticalOffset: Int): Unit

Deprecated: Deprecated in Java.

Protected methods

getOnKeyboardActionListener

Added in API level 3
protected open fun getOnKeyboardActionListener(): KeyboardView.OnKeyboardActionListener!

Deprecated: Deprecated in Java.

Returns the OnKeyboardActionListener object.

Return
KeyboardView.OnKeyboardActionListener! the listener attached to this keyboard

onAttachedToWindow

Added in API level 3
protected open fun onAttachedToWindow(): Unit

Deprecated: Deprecated in Java.

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int,int).
If you override this method you must call through to the superclass implementation.

onLongPress

Added in API level 3
protected open fun onLongPress(popupKey: Keyboard.Key!): Boolean

Deprecated: Deprecated in Java.

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
Return
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
protected open fun swipeDown(): Unit

Deprecated: Deprecated in Java.

swipeLeft

Added in API level 3
protected open fun swipeLeft(): Unit

Deprecated: Deprecated in Java.

swipeRight

Added in API level 3
protected open fun swipeRight(): Unit

Deprecated: Deprecated in Java.

swipeUp

Added in API level 3
protected open fun swipeUp(): Unit

Deprecated: Deprecated in Java.