Sunday, May 22, 2011

How To: Custom Keyboard in Android 3.0(Step 2: Displaying and Events)

Let's go on with our keyboard.


This post is telling you how to control our new keyboard in activities.


First, you need to implement two listeners called OnKeyboardActionListener and OnKeyListener like:





Next, initial your keyboard view, to apply the design you did in step one. Do this in OnCreate method:





Then add a click event to your editors that make the keyboard appearing:








Here I use a variable called currentEditText to save the touched EditText, in order to tell the keyboard which EditText should append the character:


EditText currentEditText = null; //add it in activity class before OnCreate method


Now, your apps can not be compiled because you should implement methods in Interfaces:





OK, now you can run your apps, when you touch a EditText(if you already have one), the keyboard may appears, touch again, it disappears.


But when you try to touch any key in your new keyboard, nothing happens. Now, we need to work on this.


Remember the codes I talked about on my last post? now we should have something to record the relation ship between the codes and characters.


I'm using a HashMap, surly you can use any other things you want, like, maybe, another XML file, or try to read directory from the keyboard XML file.


Add this in onKey method(or any place you want, but make sure keyCodeMap is visitable in onKey):





Then we should append charaters into the EditView when user press the key. In onKey:





what's "else" working here? codes "-5" is the delete(back) key, so we are going to delete one charater each time user press it.


OK, now the keyboard is finished, but you may find that the default Input Method is still there. We must disable it.


We have many ways to do so, here I'm using the config of the EditText:





If you have any good ideas about disabling the default Input Method, please comment and tell me.


OK, all done, test your custom keyboard in your own apps, have Fun!


Comment this post if you have any question, or you find any problem on my post.

8 comments:

  1. This is just what I need, but I have a couple problems. I am getting a "failed to instantiate KeyboardView" when I paste the xml in the layout. Also the default input method is still taking over, after I set the editable to false. I am on android 2.1. Any clues?

    ReplyDelete
  2. Re kohlsen:

    I'm doing this in android 3.0 only, but from the SDK I found that Keyboard things start since API Level 3 which means android 1.5, so if you are using android 2.1 it should not be a problem.

    I suggest you do the following checks:

    1. Make sure your project is created under android 1.5 or upper. this is not meaning which OS you are using in your device, but your project's dev target OS.

    2. Make sure you have the qwerty.xml file placed in /res/xml/ folder. or maybe you have different name for it, but make sure it's there.

    3. trying not to do any initialing things in your java class first, just place the android.inputmethodservcie.KeyboardView in your layout xml file. see if the code can run.


    For your another problem, if you set

    android:editable="false"
    android:focusable="false"

    to your EditText using the custom keyboard, it should not have any input method showing up when you touch it. OR you can try add:

    android:configChanges="keyboardHidden"

    to your activity tag under manifest xml file.

    Welcome to comment your new discovery on this.

    Thank you.

    ReplyDelete
  3. I got the custom keyboard to work by cleaning the project and setting android:editable="false", but it looks like this is a deprecated method. I also discovered that setting the inputType seems to override the custom keyboard, so I needed to remove those attributes.

    Also the error I am seeing in Eclipse when I view the Xml graphically is still there, but doesn't seem to be causing any problems. Here is the error.

    Parser is not a BridgeXmlBlockParser!
    The following classes could not be instantiated:
    - android.inputmethodservice.KeyboardView

    Do you think the app would still be stable in spite of these errors? Thanks!

    ReplyDelete
    Replies
    1. i've modified this code and it's worked perfectly in simulator with android 1.5
      And then,trouble came when i install it to my mini2440 KIT(android 1.5 OS),this keyboard was never shown
      the layout just a little bit move down,but the keyboard was not visible,through in simulator,it was shown
      What the heck is going on?

      Delete
  4. This is a wonderful example...i have been struggling with the example given in the sdk but this makes my life a lot easier...thanks a lot for this

    ReplyDelete
  5. I tried several times and it shows error in the "android.inputmethodservcie.KeyboardView " ..can somebody mail me the whole code in a zip file..,my mail id is raamkumar20@gmail.com
    please send me asap. I need to implement it in my project..Thanks

    ReplyDelete
  6. Hello, is there any keyboard xml example you've tried ? thanks

    ReplyDelete