Home » Android » Disable one key on custom keyboard in Android

Disable one key on custom keyboard in Android

Posted by: admin May 14, 2020 Leave a comment


I have custom keyboard in my app for Android. It’s layout described in xml like this

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
     <Key android:keyLabel="F1" android:keyOutputText="F1"/>
     <Key android:keyLabel="F2" android:keyOutputText="F2"/>
     <Key android:keyLabel="F3" android:keyOutputText="F3"/>

So, i’m insteresting how i can disable, for example ‘f1’ key ~ make it grey and untouchable.
There are some similar questions here, but all about default soft-KB.

I know I can iterate through keys like this

for (Keyboard.Key key : myKeyboard.getKeys())

but it’s look like objects of Keyboard.Key class are useless for any changes in key’s look.

UPD: I did not found solution. I implemented keyboard manually – big relative layout, common buttons and custom buttons and everything fine. By the way – custom keyboard at least more beautiful. Just copy resources from droid 4+ – and you’ll get nice modern transparent buttons and transparent layout on every platform.

How to&Answers:

I am currently using android:horizontalGap to place a black gap (with the width of a key) in the place where the disabled key should be placed (in this way, I can hide keys not allowed in each screen, because my keyboard has always the same distribution). It’s a poor workaround, though.

The main problem is for the rightmost keys, since android:horizontalGap can only be set for the left side of a key. But using an android:keyWidth="0" attribute in a rightmost fake key and then setting there the proper android:horizontalGap, does the trick. An even poorer workaround…

Any better solution?


Usually the keyboard is only visible if you are editing something, so you can trap the edits via the object. If its an editText box then you can add a listener and then you could disable any response to the edit text. I’m not sure if this is useful to you but at least you can trap any unwanted input.

    // add a keylistener to keep track user input
    editText.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // if keydown and "enter" is pressed
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                // do some thing or nothing

                return true;


            return false;


If Key is a Button, then it’s simple:


Otherwise, just make it not clickable:


…and change its’ appearance (background color, text color, etc.). The simplest way to do this is: