views:

124

answers:

3

I need to bring up a few different keyboards: a 'standard' keyboard with Ctrl and Alt keys; maybe a cursor pad; and so on.

I have found the Keyboard class, which would let me define a keyboard in an XML resource. I have found that the KeyboardView class has a setKeyboard method ... and, so far, I have not found any other class that takes a Keyboard instance.

How am I supposed to use the KeyboardView? I tried adding one to my activity's XML; finding it at runtime with findViewById; and then calling setKeyboard ... but all this did was mess up my layout and not bring up the special keyboard.

A: 

Keyboard and KeyboardView are for making alternative input method engines (IME). These are then able to be chosen by the user, just as they can install Swype, Graffiti, and other ones from the Android Market.

You, as a developer, can create such an IME, but you cannot force it upon the user.

CommonsWare
Huh. That's annoying - the docs sure don't hint at these being IME classes. Thanks.
Jon Shemitz
This turns out to be not true. See <http://groups.google.com/group/android-platform/browse_thread/thread/e56c8f8566a1ce7a>.
Jon Shemitz
@Jon Shemitz: What Ms. Hackborn wrote definitely is more apropos for your question. It is rather interesting that `KeyboardView` works like a normal `View` -- that certainly is not widely publicized, and I definitely was unaware of that capability. My apologies.
CommonsWare
A: 

using the inputType attribute in your editText view will help pick between the different system keyboards (phone, email, etc) Also the APIDemos application that comes with the SDK has an example of how to implement a forced custom keyboard for your app only.

schwiz
Thanks. I don't think `inputType` is quite what I need, but I'll take a look at the APIDemos app.
Jon Shemitz
OK, I guess it's time to go home ... but I've been looking at the APIDemos source (and searching through it) and I don't see anything about creating a custom keyboard. Could I trouble you for a more detailed pointer? Thanks in advance!
Jon Shemitz
my mistake sorry for leading you on a goose chase, there is the example http://code.google.com/p/android-misc-widgets/source/checkout
schwiz
Thanks for the update. That's some nice source, but it's not really what I'm looking for. I want a native Android keyboard, with my choice of keys - not something that looks and feels more or less like a native keyboard. (In a previous professional life, I saw what happened to Delphi components that looked Just Like Win9x components when XP came along.)
Jon Shemitz
A: 

This turns out to be very doable, and my initial problems probably had more to do with general Android newbiness (this is my first Android app) and not the KeyboardView. In particular, I'm used to visibility being a simple binary property.

Anyhow:

  1. Declare the KeyboardView in your XML file with android:visibility="gone".
  2. Before you make the view visible, call setKeyboard() to attach a keyboard. This is important, as the KeyboardView gets its size from the keyboard.
  3. To get raw key events, call KeyboardView.setOnKeyboardActionListener(). After refactoring this functionality from a Dialog back to my main View, I put the OnKeyboardActionListener functionality in a stand-alone class, but this is not necessary.
  4. I call keyboardView.setEnabled(true);. This does not seem to be necessary, but I am not sure (yet) under what circumstances it would matter; perhaps only if you call setEnabled(false).
  5. I call keyboardView.setPreviewEnabled(true); - this is especially useful if the user won't be getting visual feedback from an input biox right above the keyboard.
  6. Then, with the keyboard all set, call keyboardView.setVisibility(VISIBLE);.

To hide the keyboard when appropriate, just call keyboardView.setVisibility(GONE);. To change the keyboard (as on a shift key, or a cycle-through-the-symbol-keyboards key, just call setKeyboard again. I use a Map<<Integer, Keyboard> to implement a lazy-create pattern; a weak reference may be desirable, if the program will run for a long time and the keyboard will not be used much.

Jon Shemitz