Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Keyboard Mode in android custom keyboard

I have created a Custom Keyboard using the Android Keyboard Class.

I want to have Rows for two modes. One is the normal mode. And one is when the user presses the "Sym" button. For each Keyboard.Row for the "Sym" button in the XML i have specified android:keyboardMode="@+id/sym".

Now when i run it only the rows that do not specify the android:keyboardMode display. This is as expected and what the documentation says.

My questions is how do i set the Mode in my code so that the rows with android:keyboardMode="@+id/sym" get rendered?

<Row>
    <Key android:codes="113"    android:keyLabel="q" />
    <Key android:codes="119"    android:keyLabel="w" />
    <Key android:codes="101"    android:keyLabel="e" />
    <Key android:codes="114"    android:keyLabel="r" />
    <Key android:codes="116"    android:keyLabel="t" />
    <Key android:codes="121"    android:keyLabel="y" />
    <Key android:codes="117"    android:keyLabel="u" />
    <Key android:codes="105"    android:keyLabel="i" />
    <Key android:codes="111"    android:keyLabel="o" />
    <Key android:codes="112"    android:keyLabel="p" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>
<Row android:keyboardMode="@+id/sym">
    <Key android:codes="113"    android:keyLabel="+" />
    <Key android:codes="119"    android:keyLabel="_" />
    <Key android:codes="101"    android:keyLabel="=" />
    <Key android:codes="114"    android:keyLabel="%" />
    <Key android:codes="116"    android:keyLabel="^" />
    <Key android:codes="121"    android:keyLabel="|" />
    <Key android:codes="117"    android:keyLabel="&lt;" />
    <Key android:codes="105"    android:keyLabel=">" />
    <Key android:codes="111"    android:keyLabel="[" />
    <Key android:codes="112"    android:keyLabel="]" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>
like image 782
codeNinja Avatar asked Apr 23 '13 16:04

codeNinja


1 Answers

Ok figured it out myself.

There is no method to simply switch the keyboard modes. What you have to do is create 2 different keyboards and switch between them manually.

Here is what the updated XML looks like. NOTE that Rows that you want in both the keywords should not include the flag android:keyboardMode.

<!-- this is the row that shows in both modes -->
<Row android:keyWidth="51dp">
    <Key android:codes="49"    android:keyLabel="1"  />
    <Key android:codes="50"    android:keyLabel="2" />
    <Key android:codes="51"    android:keyLabel="3"  />
    <Key android:codes="52"    android:keyLabel="4"  />
    <Key android:codes="53"    android:keyLabel="5" />
    <Key android:codes="54"    android:keyLabel="6" />
    <Key android:codes="55"    android:keyLabel="7"  />
    <Key android:codes="56"    android:keyLabel="8" />
    <Key android:codes="57"    android:keyLabel="9" />
    <Key android:codes="48"    android:keyLabel="0"/>        
</Row>

<!-- this is the normal mode -->  
<Row  android:keyboardMode="@integer/keyboard_normal">
    <Key android:codes="113"    android:keyLabel="q" />
    <Key android:codes="119"    android:keyLabel="w" />
    <Key android:codes="101"    android:keyLabel="e" />
    <Key android:codes="114"    android:keyLabel="r" />
    <Key android:codes="116"    android:keyLabel="t" />
    <Key android:codes="121"    android:keyLabel="y" />
    <Key android:codes="117"    android:keyLabel="u" />
    <Key android:codes="105"    android:keyLabel="i" />
    <Key android:codes="111"    android:keyLabel="o" />
    <Key android:codes="112"    android:keyLabel="p" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>

<!-- this is the symbol mode-->
<Row  android:keyboardMode="@integer/keyboard_symbol">
    <Key android:codes="113"    android:keyLabel="+" />
    <Key android:codes="119"    android:keyLabel="_" />
    <Key android:codes="101"    android:keyLabel="=" />
    <Key android:codes="114"    android:keyLabel="%" />
    <Key android:codes="116"    android:keyLabel="^" />
    <Key android:codes="121"    android:keyLabel="|" />
    <Key android:codes="117"    android:keyLabel="&lt;" />
    <Key android:codes="105"    android:keyLabel=">" />
    <Key android:codes="111"    android:keyLabel="[" />
    <Key android:codes="112"    android:keyLabel="]" />
    <Key android:codes="-5"    android:keyIcon="@drawable/keyboard_backspace_icon" android:keyWidth="91dp" />
</Row>

Then create a integer.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="keyboard_symbol">1</integer>
    <integer name="keyboard_normal">0</integer>
</resources>

Here is the relevant java code. When you create the Keyboard object you pass the keyboard_normal or keyboard_symbol.

 normalKeyBoard = new Keyboard(activity, R.id.board, R.integer.keyboard_normal);
 symbolKeyBoard = new Keyboard(activity, R.id.board, R.integer.keyboard_symbol);

Now create a class variable to keep track of the mode. Default value is R.integer.keyboard_normal

private int mKeyboardState = R.integer.keyboard_normal;

Now in your onKeyboardActionListner().onKey method put the code to capture the key that switches the modes (assuming that you have created one in your keyboard).

        if( primaryCode== Keyboard.KEYCODE_MODE_CHANGE) {
            if(mKeyboardView != null) {
                if(mKeyboardState == R.integer.keyboard_normal){
                    //change to symbol keyboard
                    if(symbolKeyBoard== null){
                        symbolKeyBoard = new Keyboard(mHostActivity, R.xml.hexkbd, R.integer.keyboard_symbol);
                    }

                    mKeyboardView.setKeyboard(symbolKeyBoard);
                    mKeyboardState = R.integer.keyboard_symbol;
                } else {
                    if(normalKeyBoard== null){
                        normalKeyBoard = new Keyboard(mHostActivity, R.xml.hexkbd, R.integer.keyboard_normal);
                    }

                    mKeyboardView.setKeyboard(normalKeyBoard);
                    mKeyboardState = R.integer.keyboard_normal;
                }
                //no shifting
                mKeyboardView.setShifted(false);
            }
        }
like image 57
codeNinja Avatar answered Nov 09 '22 23:11

codeNinja