11

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"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"">
 <Row>
     <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.

mjollneer
  • 1,005
  • 2
  • 19
  • 36
  • @thelawnmowerman Are you trying to disable the key dynamically based on some other condition or have it be disabled all the time? – jerry Jun 20 '13 at 15:31
  • All the time! It should be easier:-) – thelawnmowerman Jun 20 '13 at 16:02
  • @thelawnmowerman I'm not sure if this would work, but how about editting the XML to add a `keyIcon` (and setting it to an image that makes it clear the button is disabled) and modifying`keyOutputText` to be an empty string? – jerry Jun 20 '13 at 17:11
  • Thanks for the idea, but It won't work, since the keyIcon attribute only changes the "superficial" icon of the button, not the button itself. The borders of the button would stay unmodified, and the touching effect would still work:-( On the other hand, I could specify a custom drawable for all the buttons, but that would be useless, I need most of the buttons as they are and only some disabled... – thelawnmowerman Jun 20 '13 at 18:06
  • @thelawnmowerman well then I think your only option (short of a full custom keyboard) would be to remove th button(s) completely (if your layou allows for it). – jerry Jun 22 '13 at 03:04
  • @jerry, read my answer below – thelawnmowerman Jun 22 '13 at 08:56
  • assign keyCode to F1. On your custom "OnKeyboardActionListener" --> "onKey", check if (primaryCode = keyCode) don't do anything on it. – Hiren Dabhi Jun 26 '13 at 13:57
  • Where do these keys tie in with the `onKey()` keycodes? – Shark Jun 26 '13 at 16:39
  • You can take a look at this : http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android/4401227#4401227 – Mehdi Karamosly Jun 26 '13 at 17:29

3 Answers3

1

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?

thelawnmowerman
  • 11,956
  • 2
  • 23
  • 36
1

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;
        }
    });
Mike Cave
  • 147
  • 1
  • 4
  • 18
-3

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

key.setEnabled(false);

Otherwise, just make it not clickable:

key.setClickable(false);

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

key.setBackgroundColor("0xFFAAAAAA");
Eric Weiss
  • 31
  • 2
  • What do you mean? I don't know what Key inherits from. It could be a Button subclass, it could be a View subclass. Naturally, View does not have as many convenience methods as Button – Eric Weiss Oct 12 '12 at 13:17
  • 1
    I mean that key's superclass isn't big secret. It's Object. Keyboard is a View, but there is no "disable(key)" or something like that. – mjollneer Oct 12 '12 at 13:29
  • 1
    hypotheticals don't really help, especially in this case where the suggestions are absolutely inapplicable. – homerman Mar 31 '16 at 23:51