Custom 'Keyboard' built in an application on Android

38,510

Solution 1

I'm not really sure if there's a straight-forward solution to this (to that extent that it is even possible to understand the real reason behind the original question).

As is quoted in the original question:

If you need some app-specific input, you should build it into your UI rather than pushing it out to a generic IME.

What is meant by that, is not that you within your app should try to build in such input features by extending or modifying the soft keyboard on the phone. There are so many different soft keyboards (and basically, the soft keyboard is just another app), since most phone manufacturers create their own version, and people download 3rd party keyboards (such as Swype or SwiftKey etc.), and I can't picture there being a way for you to "hack" into those to add a few buttons or whatever it is you want (which could also be a major security hole, another reason why it probably isn't possible).

What instead the above quote suggests, is that you have to create some other form of input besides the keyboard. One such example, and a very good one if I might add, is how the RealCalc Scientific Calculator looks:

RealCalc Scientific Calculator

Now this isn't open source, so I can only guess how the code looks like (but it shouldn't be too hard a guess either): in its simplest form, this is just a grid with lots of buttons. Each button handles the onClick event, which would mean performing some kind of action (changing the label on some other buttons, showing a menu, displaying some text in the upper label or whatever), and that's probably pretty much what's to it. And of course, the phone's soft keyboard is never displayed (since you don't need a keyboard with all those buttons (and also there aren't any input fields to write anything in)).

It all boils down to the already mentioned quote: If you need some app-specific input, you should build it into your UI. Or in other words: create buttons (and don't display the soft keyboard if you don't need it) and make things happen when you click them.


And just to have mentioned it: if you do want to create your own IME (which I strongly believe is not the case here), you should have a look at the following resources:

Solution 2

In my humble opinion you should take a look at the beginning of reference about keyboard and keyboard view http://developer.android.com/reference/android/inputmethodservice/Keyboard.html and http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html.

There you can see an example of defining keyboard using XML file. I think that this is what you are looking for.

Solution 3

As mentioned by @sebap123 Keyboard and KeyboardView class are the one you need to use,

Further, for Implementation, here is a quick detailed guide.

Share:
38,510
OlivierM
Author by

OlivierM

Updated on April 07, 2020

Comments

  • OlivierM
    OlivierM about 4 years

    I've been looking to create a custom keyboard for my application. At first, I started to look at the SoftKeyboard for the SDK examples, but reading the Android Developer Group led me to this post:

    This is really not how the input method framework is supposed to work. An IME should be a generic input facility, not for a particular application. If you need some app-specific input, you should build it into your UI rather than pushing it out to a generic IME.

    How do I build an app-specific input within the UI? I mean, is there a way to extend the Keyboard app or something and use it only in my application?

    Features needed for the keyboard:

    • Shift key to display some other keys
    • Special keys like square root or PI
    • etc.

    PS: an ugly solution could be to make a table of ImageButton for example, but I wanted to make something clean.

  • Danny
    Danny over 12 years
    Looks promising. Looks like code.google.com/p/shortyz/source/browse does this.
  • TWiStErRob
    TWiStErRob over 9 years
    The OP wants a math keyboard and the built-in ones are ill-suited for writing in that context (try writing 1 line of Java with some math, it is hell). The solution proposed here doesn't work if you have multiple EditTexts/different activities for example and want to enter math into them (and entering math isn't the primary purpose of neither of those UIs). I myself am looking to make entering hexa colors easier by not only restricting/filtering the keys, but only providing/showing the possible ones... and this is just 1/6th of the UI so can't put the full keyboard in the activity as shown here
  • TWiStErRob
    TWiStErRob over 9 years
    KeyboardView IS the answer, the question is how you integrate it. Because if you create an IME, it's public and user has to select it. If you don't create, just put the view into the layout, then it's really hacky, see all the different listeners needed. It may be lack of knowledge, or impossible integration design...
  • Poutrathor
    Poutrathor over 8 years
    it is really hacky. It was shipped with earliest android but it never got upgraded. Thus, its behavior is terrible. We work 2 weeks to get something close to good, but in the end, we are keeping the code and putting it in a custom IME app.
  • Starwave
    Starwave over 5 years
    Thanks for the shortyz link, checked the code and Keyboard is integrated only in app, not accessable in other apps, which is what I was looking for.