I’ve recently had a problem with Android Keyboard views.
What I wanted to achieve was a decent keyboard, where I could add key spacing in pixels and the rest of the space would be shared by the keys depending on how I weighted them.
I also found percentage widths were inaccurate (rows ends varied by +-6px).
Each key is given a width, usually 64 pixels. For a half-normal width key I would assign 32px. For double I gave it 128px. Etc.
The keyboard is then run through a function called “fixKeyboard” which decides upon the width for each key. You tell it how many 64px width keys per row, and it will scale the keys to a size that works. (i.e keysPerStandardRow == how many pixels wide the keyboard is / 64px)
Alas, this was not the end of the story. It seems that the keyboard view/keyboard decides upon the width of the keyboard the moment you hand it the XML file. If for some reason you’ve changed the keys like I have, android scales them back into its pre-calculated box. I didn’t want this, obviously. So here’s what I did: I simply forced it to have the maximum possible width by defining a huge horizontal gap in between each key (424242px), which I then reset to 0 when running fixKeyboard.
Since I was using this approach, you can define gaps easily by simply not using the trigger number! Here’s my QWERTY keyboard you can use if you’d like as an example:
A few important things: to make a gap BEFORE a key, I just added a 0 width key.
<Key android:keyLabel=" " android:keyHeight="0px" android:keyWidth="0px" android:horizontalGap="32px"/>Another thing: For the control keys (Like the one that opens the IME menu, I’ve used special characters to alert the code:
android:keyOutputText="◐"etc.It took me a while to search for the way to open the IME menu, so here’s how:
How to set/call an new input method in Android
HTH, all code is public domain