japk
japk

Reputation: 629

Android Keyboard input predefined text

I have been following this tutorial on making a custom keyboard. So far so good.

However, I wanted to know, is it possible to create a button to input predefined text. Eg. A button labeled "Name" which inputs the user's name? If it is possible, how do I do it? I have done extensive research, but can find nothing.

Upvotes: 1

Views: 303

Answers (1)

DmitryArc
DmitryArc

Reputation: 4837

Yes, it's possible. In your implementation of InputMethodService define new keycode

private final static int NAME_CODE = -32; // value is absolutely random. The main requirement - there should not be coincidence with other codes

and later check for it in the onKey() method

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
        switch(primaryCode){
        .....
        case NAME_CODE:
            ic.commitText(name, name.length());
            break;
        .....

And after that just use this code to bind special key from your Keyboard specification (qwerty.xml in example) and the logic.

<Key android:codes="-32" android:keyLabel="NAME" android:keyWidth="20%p" android:isRepeatable="true"/>

Upvotes: 1

Related Questions