0

I know how to create a keyboard from a xml file,looks like the following:

<Row>
<Key          android:codes="113"android:keyLabel="q"android:keyEdgeFlags="left"/>
<Key android:codes="119" android:keyLabel="w"/>
<Key android:codes="101" android:keyLabel="e"/>
......

but I want to use a custom layout,like this:

<LinearLayout ......>
<Button android:id="@+id/key_q" .../>
<Button android:id="@+id/key_w" .../>
</LinearLayout>

How can I make the button as a key?

Inso
  • 837
  • 1
  • 7
  • 11
  • 1
    Possible duplicate of [Custom 'Keyboard' built in an application on Android](http://stackoverflow.com/questions/3196511/custom-keyboard-built-in-an-application-on-android) – Elltz Mar 10 '16 at 03:09

2 Answers2

3

In the onCreateInputView of your InputMethodService, you can return any view you want, and it will be the main view of your keyboard. You aren't restricted to KeyboardView, in fact no major keyboard actually uses that, it's too limiting. Inflate whatever you want and return it from onCreateInputView.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • So should I create a class extends android.inputmethodservice.Keyboard? because I think Keyboard.class is work with KeyboardView.class, thanks for helping me. – Inso Mar 10 '16 at 09:29
  • No, android.inputmethodservice.InputMethodService. That's the base class for any keyboard. Keyboard and Keyboard View are shortcuts for making very simple keyboards, but if you're doing anything complex you won't use them- they aren't flexible enough. – Gabe Sechan Mar 10 '16 at 16:09
  • Thanks for your help! – Inso Mar 11 '16 at 03:02
  • this is a fantastic answer. thanks for the simple explanation. I'm assuming the returned view doesn't require too much special configuration in order for it to display correctly? that is to say, it will properly appear at the bottom of the device and react with rotation accordingly – keeehlan Jun 02 '16 at 18:06
  • Yes- Android will make it appear at the bottom and rotation is the standard android rotation. If you want it to appear elsewhere you can make that happen, through very careful manipulation of onComputeInsets which is a rarely used function that can set what region of screen a keyboard appears on and captures touches from. – Gabe Sechan Jun 02 '16 at 18:13
0

You'll need to create a class that extends of KeyboardView and set the layout you want. This way you can make the layout any way you want just like any other layout.

See this thread to get some codes.

Community
  • 1
  • 1