21

I'd like to write an custom keyboard which should work on all devices that are running Android 4.0 and up. So first I searched the net but didn't found anything about that. So how can I create an app which replaces the stock keyboard of Android?

What I'd like to know:

  1. Is there a good tutorial out there? Do you guys have sample code?
  2. Do I need root to do this?
  3. What's the structure behind it? (Is it just a regular Activity with a Service??)
  4. Is it possible to read out the Inputbox within the keyboard?
hichris123
  • 10,145
  • 15
  • 56
  • 70
safari
  • 7,565
  • 18
  • 56
  • 82

3 Answers3

20

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

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • allright, so its getting very hard when i'd like to have in my keyboard some kind of spinnerbox or something, which let the user change the text? Am I right in this? Or can i just overlay a new layout in there? – safari Jan 11 '13 at 19:13
  • SO the idea behind that part of the framework is that you'd be implementing the entire keyboard- tapping, prediction, etc. This includes drawing. So adding a spinner shouldn't be a problem, despite the fact you're a service the IME is allowed to put up a UI (the only such service). So just put the spinner in your layout. If you just want a standard keyboard with no prediction in there there's a lot of help in Android, but I'm not sure how much- we did everything custom, our layout was just a custom view and we drew it ourselves. – Gabe Sechan Jan 11 '13 at 19:17
  • allright, so I'll give this a shot, btw. do you know about any other example code except the SoftKey which is from Google? – safari Jan 11 '13 at 19:18
  • There's probably a small tutorial or two, but not much. If you go into the AOSP code you can probably find android's keyboard. It used to be called "latinime". That's your best example, and you may be able to alter it to your liking. – Gabe Sechan Jan 11 '13 at 19:20
9

So first I searched the net but didn't found anything about that

Creating an input method is covered in the Android developer documentation, which is on the Internet.

Do you guys have samplecode?

There is a sample in the samples/ directory of your SDK installation, if you downloaded sample code from the SDK Manager. Look for SoftKeyboard.

Do I need root to do this?

No.

What's the structure behind it? (Is it just a regular Activity with a Service??)

It is an InputMethodService.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • hmm didn't found it in the developer documentation, btw @CommonsWare is this topic covered in one of your books? – safari Jan 11 '13 at 18:49
  • @safari: No, sorry. It's on the list, but it's not a high priority, because it is a rather specialized topic. – CommonsWare Jan 11 '13 at 18:54
  • allright =), to the last question, is it possible that I can read out what I wrote into the inputbox (edittext whatever), and replace it? – safari Jan 11 '13 at 18:59
  • @safari: I am not familiar with how the `InputMethodService` works with respect to editing existing text, sorry. – CommonsWare Jan 11 '13 at 19:03
  • @CommonsWare: thanks for the answer. I have used the SoftKeyboard(given in the android sdk sample). But the auto-suggest bar is not showing in the keyboard. How can I show the auto-suggest words in that SoftKeyboard. – SKK Apr 09 '14 at 10:39
  • @karthi: I have no idea, sorry. – CommonsWare Apr 09 '14 at 11:08
  • @CommonsWare: ok thanks for the response. And one more doubt. Is it possible to customize that SoftKeyboard. That is If I want to do some action, when the use long press a key. – SKK Apr 09 '14 at 12:25
  • @karthi: Presumably yes, though I have not tried it. – CommonsWare Apr 09 '14 at 13:05
  • @CommonsWare I would like to use that softkeyboard in my apps keyboard, how can I display that keyboard in activity with out adding it as input service.. – Roster Oct 21 '15 at 08:15
2
  1. Is there a good tutorial out there? Do you guys have sample code?

Hereby a good tutorial:
http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

  1. Do I need root to do this?

No, root is not required to do this.

  1. What's the structure behind it? (Is it just a regular Activity with a Service??)

You need to use the inputMethodService, see the documentation.

  1. Is it possible to read out the Inputbox within the keyboard?

root: no

Is it possible to read out the Inputbox within the keyboard?

yes it is possible.

InputConnection ic = getCurrentInputConnection();
String currentText = ic.getExtractedText(new ExtractedTextRequest(), 0).text;
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
Fay007
  • 2,707
  • 3
  • 28
  • 58