If you build applications that require input from keyboard characters or if you want to create your own keyboard, you need to know more about the methods and classes that can be called to use the keyboard. I have to mention that this article is intended primarily to touchscreen phones without a physical keyboard and for slide phones that has also touchscreen. In the following, you will find several examples on how to use the properties for a virtual keyboard.

A view that renders a virtual keyboard. It handles rendering of keys and detecting key presses and touch movements.
<ol>
<li>package android.inputmethodservice;</li>
<li>import com.android.internal.R;</li>
<li>import android.content.Context;</li>
<li>import android.content.SharedPreferences;</li>
<li>import android.content.res.TypedArray;</li>
<li>import android.graphics.Canvas;</li>
<li>import android.graphics.Paint;</li>
<li>import android.graphics.Rect;</li>
<li>import android.graphics.Typeface;</li>
<li>import android.graphics.Paint.Align;</li>
<li>import android.graphics.drawable.Drawable;</li>
<li>import android.inputmethodservice.Keyboard.Key;</li>
<li>import android.os.Handler;</li>
<li>import android.os.Message;</li>
<li>import android.os.Vibrator;</li>
<li>import android.preference.PreferenceManager;</li>
<li>import android.util.AttributeSet;</li>
<li>import android.view.GestureDetector;</li>
<li>import android.view.Gravity;</li>
<li>import android.view.LayoutInflater;</li>
<li>import android.view.MotionEvent;</li>
<li>import android.view.View;</li>
<li>import android.view.ViewConfiguration;</li>
<li>import android.view.ViewGroup.LayoutParams;</li>
<li>import android.widget.Button;</li>
<li>import android.widget.PopupWindow;</li>
<li>import android.widget.TextView;</li>
<li>import java.util.Arrays;</li>
<li>import java.util.HashMap;</li>
<li>import java.util.List;</li>
<li>import java.util.Map;</li>
<li>/**</li>
<li>* A view that renders a virtual {@link Keyboard}. It handles rendering of keys and</li>
<li>* detecting key presses and touch movements.</li>
<li>*</li>
<li>* @attr ref android.R.styleable#KeyboardView_keyBackground</li>
<li>* @attr ref android.R.styleable#KeyboardView_keyPreviewLayout</li>
<li>* @attr ref android.R.styleable#KeyboardView_keyPreviewOffset</li>
<li>* @attr ref android.R.styleable#KeyboardView_labelTextSize</li>
<li>* @attr ref android.R.styleable#KeyboardView_keyTextSize</li>
<li>* @attr ref android.R.styleable#KeyboardView_keyTextColor</li>
<li>* @attr ref android.R.styleable#KeyboardView_verticalCorrection</li>
<li>* @attr ref android.R.styleable#KeyboardView_popupLayout</li>
<li>*/</li>
<li>public class KeyboardView extends View implements View.OnClickListener {</li>
<li>/**</li>
<li>* Listener for virtual keyboard events.</li>
<li>*/</li>
<li>public interface OnKeyboardActionListener {</li>
<li>/**</li>
<li>* Called when the user presses a key. This is sent before the {@link #onKey} is called.</li>
<li>* For keys that repeat, this is only called once.</li>
<li>* @param primaryCode the unicode of the key being pressed. If the touch is not on a valid</li>
<li>* key, the value will be zero.</li>
<li>* @hide Pending API Council approval</li>
<li>*/</li>
<li>void onPress(int primaryCode);</li>
<li>/**</li>
<li>* Called when the user releases a key. This is sent after the {@link #onKey} is called.</li>
<li>* For keys that repeat, this is only called once.</li>
<li>* @param primaryCode the code of the key that was released</li>
<li>* @hide Pending API Council approval</li>
<li>*/</li>
<li>void onRelease(int primaryCode);</li>
<li>/**</li>
<li>* Send a key press to the listener.</li>
<li>* @param primaryCode this is the key that was pressed</li>
<li>* @param keyCodes the codes for all the possible alternative keys</li>
<li>* with the primary code being the first. If the primary key code is</li>
<li>* a single character such as an alphabet or number or symbol, the alternatives</li>
<li>* will include other characters that may be on the same key or adjacent keys.</li>
<li>* These codes are useful to correct for accidental presses of a key adjacent to</li>
<li>* the intended key.<!--more--></li>
<li>*/</li>
<li>void onKey(int primaryCode, int[] keyCodes);</li>
<li>/**</li>
<li>* Called when the user quickly moves the finger from right to left.</li>
<li>*/</li>
<li>void swipeLeft();</li>
<li>/**</li>
<li>* Called when the user quickly moves the finger from left to right.</li>
<li>*/</li>
<li>void swipeRight();</li>
<li>/**</li>
<li>* Called when the user quickly moves the finger from up to down.</li>
<li>*/</li>
<li>void swipeDown();</li>
<li>/**</li>
<li>* Called when the user quickly moves the finger from down to up.</li>
<li>*/</li>
<li>void swipeUp();</li>
<li>}</li>
<li>private static final boolean DEBUG = false;</li>
<li>private static final int NOT_A_KEY = -1;</li>
<li>private static final int[] KEY_DELETE = { Keyboard.KEYCODE_DELETE };</li>
<li>private static final int[] LONG_PRESSABLE_STATE_SET = { R.attr.state_long_pressable };</li>
<li>private Keyboard mKeyboard;</li>
<li>private int mCurrentKeyIndex = NOT_A_KEY;</li>
<li>private int mLabelTextSize;</li>
<li>private int mKeyTextSize;</li>
<li>private int mKeyTextColor;</li>
<li>private float mShadowRadius;</li>
<li>private int mShadowColor;</li>
<li>private float mBackgroundDimAmount;</li>
<li>private TextView mPreviewText;</li>
<li>private PopupWindow mPreviewPopup;</li>
<li>private int mPreviewTextSizeLarge;</li>
<li>private int mPreviewOffset;</li>
<li>private int mPreviewHeight;</li>
<li>private int[] mOffsetInWindow;</li>
<li>private PopupWindow mPopupKeyboard;</li>
<li>private View mMiniKeyboardContainer;</li>
<li>private KeyboardView mMiniKeyboard;</li>
<li>private boolean mMiniKeyboardOnScreen;</li>
<li>private View mPopupParent;</li>
<li>private int mMiniKeyboardOffsetX;</li>
<li>private int mMiniKeyboardOffsetY;</li>
<li>private Map<Key,View> mMiniKeyboardCache;</li>
<li>private int[] mWindowOffset;</li>
<li>/** Listener for {@link OnKeyboardActionListener}. */</li>
<li>private OnKeyboardActionListener mKeyboardActionListener;</li>
<li>private static final int MSG_REMOVE_PREVIEW = 1;</li>
<li>private static final int MSG_REPEAT = 2;</li>
<li>private static final int MSG_LONGPRESS = 3;</li>
<li>private int mVerticalCorrection;</li>
<li>private int mProximityThreshold;</li>
<li>private boolean mPreviewCentered = false;</li>
<li>private boolean mShowPreview = true;</li>
<li>private boolean mShowTouchPoints = false;</li>
<li>private int mPopupPreviewX;</li>
<li>private int mPopupPreviewY;</li>
<li>private int mLastX;</li>
<li>private int mLastY;</li>
<li>private int mStartX;</li>
<li>private int mStartY;</li>
<li>private boolean mProximityCorrectOn;</li>
<li>private Paint mPaint;</li>
<li>private Rect mPadding;</li>
<li>private long mDownTime;</li>
<li>private long mLastMoveTime;</li>
<li>private int mLastKey;</li>
<li>private int mLastCodeX;</li>
<li>private int mLastCodeY;</li>
<li>private int mCurrentKey = NOT_A_KEY;</li>
<li>private long mLastKeyTime;</li>
<li>private long mCurrentKeyTime;</li>
<li>private int[] mKeyIndices = new int[12];</li>
<li>private GestureDetector mGestureDetector;</li>
<li>private int mPopupX;</li>
<li>private int mPopupY;</li>
<li>private int mRepeatKeyIndex = NOT_A_KEY;</li>
<li>private int mPopupLayout;</li>
<li>private boolean mAbortKey;</li>
<li>private Key mInvalidatedKey;</li>
<li>private Rect mClipRegion = new Rect(0, 0, 0, 0);</li>
<li>private Drawable mKeyBackground;</li>
<li>private static final int REPEAT_INTERVAL = 50; // ~20 keys per second</li>
<li>private static final int REPEAT_START_DELAY = 400;</li>
<li>private static final int LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout();</li>
</ol>
[view the entire code here]
Listener for virtual keyboard events. How to use public static interface KeyboardView.OnKeyboardActionListener properties.
public void onKey(int primaryCode, int[] keyCodes)
Send a key press to the listener.
Parameters:
@param primaryCode this is the key that was pressed
@param keyCodes the codes for all the possible alternative keys with the primary code being the first. If the primary key code is a single character such as an alphabet or number or symbol, the alternatives will include other characters that may be on the same key or adjacent keys. These codes are useful to correct for accidental presses of a key adjacent to the intended key.
public void onPress(int primaryCode)
Called when the user presses a key. This is sent before the android.inputmethodservice.KeyboardView$OnKeyboardActionListener.onKey(int, int[]) is called. For keys that repeat, this is only called once.
Parameters:
@param primaryCode the unicode of the key being pressed. If the touch is not on a valid key, the value will be zero.
public void onRelease(int primaryCode)
Called when the user releases a key. This is sent after the android.inputmethodservice.KeyboardView$OnKeyboardActionListener.onKey(int, int[]) is called. For keys that repeat, this is only called once.
Parameters:
@param primaryCode the code of the key that was released
public void onText(CharSequence text)
Sends a sequence of characters to the listener.
Parameters:
@param text the sequence of characters to be displayed.
public void swipeDown()
Called when the user quickly moves the finger from up to down.
public void swipeLeft()
Called when the user quickly moves the finger from right to left.
public void swipeRight()
Called when the user quickly moves the finger from left to right.
public void swipeUp()
Called when the user quickly moves the finger from down to up.
developer.android
A view that renders a virtual Keyboard. It handles rendering of keys and detecting key presses and touch movements.
Read more about XML attributes for a virtual keyboard here.