Ashwin Chandlapur
Ashwin Chandlapur

Reputation: 11

Android SoftKeyboard: Shiftkey to load another keyboad layout

I have two files where I have android KeyCodes: Qwerty.xml and numbers.xml. On any press of shift key, I want the numeric keypad to be displayed.

Java Class:

public class MyKeyboard extends InputMethodService
                        implements KeyboardView.OnKeyboardActionListener
{
    private KeyboardView kv;
    private Keyboard keyboard;
    private boolean caps = false;

    @Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.qwerty);
        //keyboard = new Keyboard(this,R.xml.qwerty1);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
        switch (primaryCode){
            case Keyboard.KEYCODE_DELETE:
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                caps = !caps;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char) primaryCode;
                if (Character.isLetter(code) && caps) {
                   code = Character.toUpperCase(code);
                }
                ic.commitText(String.valueOf(code), 1);
        }
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeLeft() {
    }

    @Override
    public void swipeRight() {
    }

    @Override
    public void swipeDown() {
    }

    @Override
    public void swipeUp() {
    }
}

Is there any method that I can call from case Keyboard.KEYCODE_SHIFT in order to load numbers.xml file to display numeric keypad?

Upvotes: 1

Views: 800

Answers (2)

Mika
Mika

Reputation: 41

Try to change your

            case Keyboard.KEYCODE_SHIFT:
            caps = !caps;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            break;

to

            case Keyboard.KEYCODE_SHIFT:
            keyboard = new Keyboard(this, R.xml.numbers);
            kv.setKeyboard(keyboard);
            kv.setOnKeyboardActionListener(this);
            break;

Or add in to your qwerty.xml the shift keycode to eg. -1 and add this to your java class

            case -1:
            keyboard = new Keyboard(this, R.xml.number);
            kv.setKeyboard(keyboard);
            kv.setOnKeyboardActionListener(this);
            break;

Upvotes: 1

Maihan Nijat
Maihan Nijat

Reputation: 9355

Edit both Qwerty.xml and numbers.xml file and assign custom keyCode to shift key. For example: The keyCode of shift key is 120000.

Make an instance of LatinKeyboard:

private LatinKeyboard numbersKeyboard;

And add logic to the following method (SoftKeyboard.java):

public void onKey(int primaryCode, int[] keyCodes) {
.....

if (primaryCode == 120000) {
    mInputView.setKeyboard(numbersKeyboard);
}

...

Upvotes: 0

Related Questions