How to Make a Custom Keyboard in Android

Create Custom Keyboard in Android

So, I did this for about 2 years, when I worked on Swype. You don't need root, you just need to implement an InputMethodService. You can get the text out of most textboxes, but not all (not all edit fields correctly implement their half of the APIs. Particularly anything with an input type INPUT_TYPE_NULL will not work well). To get the text you would call inputConnection.getExtractedText

Be warned- the API is bad, and apps are hit and miss on working with it. A basic keyboard is easy, but try and do anything complex and you'll spend a lot of time. A lot of man hours went in to the big keyboards

Creating custom keyboard for android

To customize the keyboard you will need to modify your "qwerty" in the xml folder and the keyboard on the layout folder. I'll show some examples:

This goes on the layout folder

<?xml version="1.0" encoding="UTF-8"?>
<com.example.keyboard.MyKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:keyPreviewLayout="@layout/preview"
android:keyBackground="@drawable/key_selector"
android:shadowRadius="0.0"
android:keyTextColor="#000000"
/>

And this goes on the xml folder:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:keyHeight="8%p">

<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
<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" />
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
<Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left" android:horizontalGap="5%p" />
<Key android:codes="115" android:keyLabel="s" />
<Key android:codes="100" android:keyLabel="d" />
<Key android:codes="102" android:keyLabel="f" />
<Key android:codes="103" android:keyLabel="g" />
<Key android:codes="104" android:keyLabel="h" />
<Key android:codes="106" android:keyLabel="j" />
<Key android:codes="107" android:keyLabel="k" />
<Key android:codes="108" android:keyLabel="l" />
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
<Key android:codes="3" android:keyIcon="@drawable/keyboard_shift_off_normal"
android:keyWidth="13.7%p"/>
<Key android:codes="122" android:keyLabel="z" android:horizontalGap="1%p"/>
<Key android:codes="120" android:keyLabel="x" />
<Key android:codes="99" android:keyLabel="c" />
<Key android:codes="118" android:keyLabel="v" />
<Key android:codes="98" android:keyLabel="b" />
<Key android:codes="110" android:keyLabel="n" />
<Key android:codes="109" android:keyLabel="m" />
<Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete_dim"
android:keyWidth="13.7%p"
android:horizontalGap="1%p"/>
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
<Key android:codes="-16" android:keyIcon="@drawable/keyboard_symbol"
android:keyWidth="18.7%p"/>
<Key android:codes="44" android:keyLabel="," android:horizontalGap="1%p"/>
<Key android:codes="32" android:keyIcon="@drawable/sym_keyboard_feedback_space" android:keyWidth="40%p"/>
<Key android:codes="46" android:keyLabel="."/>
<Key android:codes="-3" android:keyIcon="@drawable/keyboard_go"
android:keyWidth="18.5%p" android:horizontalGap="1%p"/>
</Row>
</Keyboard>

Like you said, you can extend a class from keyboard layout, but it's more for customize keyboard events, like onTouch, onLongKeyPress,

How to create a custom Keyboard?

Keyboard, according to the deprecation message, is just a UI widget class.

This class was deprecated in API level 29.
This class is deprecated because this is just a convenient UI widget class that application developers can re-implement on top of existing public APIs. If you have already depended on this class, consider copying the implementation from AOSP into your project or re-implementing a similar widget by yourselves

If you want to follow the tutorials that use it, just copy the class from the google source into your project and use it locally. Otherwise, you can customise it by creating it from scratch and giving the widget the look and feel that you want.

How do I create a custom Android keyboard with numbers-only?

I now went for using no keyboard and using GridLayout instead.

<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:columnCount="3"
android:orientation="horizontal"
android:weightSum="3">

<Button
android:layout_columnWeight="1"
android:text="1" />

<Button
android:layout_columnWeight="1"
android:text="2" />

<Button
android:layout_columnWeight="1"
android:text="3" />

<Button
android:layout_columnWeight="1"
android:text="4" />

<Button
android:layout_columnWeight="1"
android:text="5" />

<Button
android:layout_columnWeight="1"
android:text="6" />

<Button
android:layout_columnWeight="1"
android:text="7" />

<Button
android:layout_columnWeight="1"
android:text="8" />

<Button
android:layout_columnWeight="1"
android:text="9" />

<Button
android:layout_columnWeight="1"
android:layout_column="1"
android:text="0" />
</GridLayout>

Create custom keyboard in android using different layout and keys not being rectangle, shape picked from the drawable image

I used the ImageButton and specified a source file for the drawable image.

This helped me with the image on the button, but the button still occupied a rectangle section. To remove the alpha region, I seeked help of the android:background property of ImageButton in android layout file.

    android:background="@android:color/transparent"
android:src="@drawable/your_image"

Now, if you want to disallow button taps on those regions, you can refer the following link.
Hexagon button with hexagon touch area

Prevent the use of Custom Keyboard

It seems there's no direct way in doing this. The easiest one (not that easy actually) is by collecting all of the list of default keyboard in each model/brand of phones, and validate within it. This was also the solution of one of the private security SDK that I've known.

The other solution (thanks to @Garren Fitzenreiter) is by creating a custom keyboard in the app and use that instead.

And lastly, the easiest one (this is really easy, because this is not an actual solution haha) is just by putting it inside your app's terms and conditions indicating that the user should avoid any custom keyboards.



Related Topics



Leave a reply



Submit