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

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

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

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

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

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)

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

Public Methods

public void closing ()

public Keyboard getKeyboard ()

Returns the current keyboard being displayed by this view.

Returns
  • the currently attached keyboard

public boolean handleBack ()

public boolean isPreviewEnabled ()

Returns the enabled state of the key feedback popup.

Returns
  • whether or not the key feedback popup is enabled

public boolean isProximityCorrectionEnabled ()

Returns true if proximity correction is enabled.

public boolean isShifted ()

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

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)

Implement this to do your drawing.

Parameters
canvas the canvas on which the background will be drawn

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

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)

Implement this method to handle touch screen motion events.

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

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

public void setOnKeyboardActionListener (KeyboardView.OnKeyboardActionListener listener)

public void setPopupOffset (int x, int y)

public void setPopupParent (View v)

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

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

public boolean setShifted (boolean shifted)

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)

Protected Methods

protected KeyboardView.OnKeyboardActionListener getOnKeyboardActionListener ()

Returns
  • the listener attached to this keyboard

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

protected void swipeLeft ()

protected void swipeRight ()

protected void swipeUp ()