KeyboardView


public class KeyboardView
extends View implements View.OnClickListener

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


This class was deprecated in API level 29.
This class is deprecated because this is just a convenient UI widget class that application developers can re-implement on top of existing public APIs. If you have already depended on this class, consider copying the implementation from AOSP into your project or re-implementing a similar widget by yourselves

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: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

Inherited fields

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 OnKeyboardActionListener#onKey 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 OnKeyboardActionListener object.

void onAttachedToWindow()

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

boolean onLongPress(Keyboard.Key popupKey)

Called when a key is long pressed.

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

Inherited methods

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.

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

android:keyPreviewHeight

Height of the key press feedback popup.

May 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), and mm (millimeters).

android:keyPreviewLayout

Layout resource for key press feedback.

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

android:keyPreviewOffset

Vertical offset of the key press feedback from the key.

May 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), and mm (millimeters).

android:keyTextColor

Color to use for the label in a key.

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

android:keyTextSize

Size of the text for character keys.

May 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), and mm (millimeters).

android:labelTextSize

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

May 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), and mm (millimeters).

android:popupLayout

Layout resource for popup keyboards.

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

android:verticalCorrection

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

May 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), and mm (millimeters).

Public constructors

KeyboardView

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

Parameters
context Context

attrs AttributeSet

KeyboardView

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

KeyboardView

Added in API level 21
public 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
public void closing ()

getKeyboard

Added in API level 3
public Keyboard getKeyboard ()

Returns the current keyboard being displayed by this view.

Returns
Keyboard the currently attached keyboard

handleBack

Added in API level 3
public boolean handleBack ()

Returns
boolean

invalidateAllKeys

Added in API level 4
Deprecated in API level 29
public void invalidateAllKeys ()

Requests a redraw of the entire keyboard. Calling View.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
Deprecated in API level 29
public 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.

isPreviewEnabled

Added in API level 3
public boolean isPreviewEnabled ()

Returns the enabled state of the key feedback popup.

Returns
boolean whether or not the key feedback popup is enabled

isProximityCorrectionEnabled

Added in API level 3
public boolean isProximityCorrectionEnabled ()

Returns true if proximity correction is enabled.

Returns
boolean

isShifted

Added in API level 3
public 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.

onDetachedFromWindow

Added in API level 3
public void onDetachedFromWindow ()

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
public void onDraw (Canvas canvas)

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

setOnKeyboardActionListener

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

Parameters
listener KeyboardView.OnKeyboardActionListener

setPopupOffset

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

Parameters
x int

y int

setPopupParent

Added in API level 3
public void setPopupParent (View v)

Parameters
v View

setPreviewEnabled

Added in API level 3
public 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
public void setProximityCorrectionEnabled (boolean enabled)

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
public 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
public void setVerticalCorrection (int verticalOffset)

Parameters
verticalOffset int

Protected methods

getOnKeyboardActionListener

Added in API level 3
protected KeyboardView.OnKeyboardActionListener getOnKeyboardActionListener ()

Returns the OnKeyboardActionListener object.

Returns
KeyboardView.OnKeyboardActionListener the listener attached to this keyboard

onAttachedToWindow

Added in API level 3
protected void onAttachedToWindow ()

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 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
protected void swipeDown ()

swipeLeft

Added in API level 3
protected void swipeLeft ()

swipeRight

Added in API level 3
protected void swipeRight ()

swipeUp

Added in API level 3
protected void swipeUp ()