18

I'm creating a simple typing game in Android. I have no problem getting input from the physical keyboard, but now I'm trying to get the soft keyboard to appear without the EditText. So far, I've tried the following:

1. EditText with visibility="invisible" and this line:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(keyboard_edittext, InputMethodManager.SHOW_FORCED); // SHOW_IMPLICIT also failed

2. This line in the onCreate():

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

This method actually displayed an empty white box across the bottom 10% of the screen but not the keyboard, although when I run it now it does nothing.

3. Another two lines in the onCreate():

InputMethodManager m = (InputMethodManager)this.getSystemService (Context.INPUT_METHOD_SERVICE); m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);

No luck on any of these. Is it even possible to display the soft keyboard (and then use onKeyUp/onKeyDown) without focusing on an EditText?

Right now, the only way I can see is to approach this is to create my own implementation of the soft keyboard (i.e. build it from scratch). Not looking forward to that!

slm
  • 15,396
  • 12
  • 109
  • 124
ang
  • 183
  • 2
  • 7
  • Even if you manage to show the soft keyboard, onKeyUp()/Down() won't get called - at least not for all keys and on all Android versions. In some Android (older) versions (I think 1.5 and maybe 1.6) onKeyDown() is called by numeric keys, but no letters. Nevertheless, in general using onKeyDown() for receiving soft keyboard input won't lead you to the desired results - this will only work properly using hard keyboards. – ubuntudroid Jun 15 '11 at 14:27

5 Answers5

6

The following code works for me:

 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
 imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

I use this code in a onKeyUp handler when I press the "menu" button.

miha
  • 3,287
  • 3
  • 29
  • 44
  • 1
    Is there a way to force a certain type of input in that case? like only digits.. instead of using `setRawInputType ` – Mohamed Khamis Feb 27 '15 at 15:30
  • 1
    @MohamedKhamis, I'm not sure. If you have a TextEdit input on which you active the keyboard, then yes, you can set imeType as describer in the documentation (https://developer.android.com/training/keyboard-input/style.html) – miha Feb 27 '15 at 19:56
5

Instead of using visibility="invisible" you can set android:alpha="0" on your EditText. So you still need a EditText but it is not visible and you can get the input from the softkeyboard by an onKeyListener()

Terel
  • 3,857
  • 1
  • 25
  • 28
2

You can force the Softkeyboard to be shown by using:


InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(myView, InputMethodManager.SHOW_FORCED);

Phyrum Tea
  • 2,623
  • 1
  • 19
  • 20
  • No go changing to SHOW_FORCED. Also tried requestFocus(); on the edittext but that didn't work either. – ang Jan 02 '11 at 23:05
2

Make sure to enable the soft keyboard for your view:

setFocusable(true);
setFocusableInTouchMode(true);

Then call:

InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Tod
  • 4,618
  • 4
  • 32
  • 23
1

Note that if you are working in landscape mode, the soft input will create its own text input field ruining all your hard work. You can prevent this behavior:

// This makes us remain invisible when in landscape mode.
setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

Now if you have set up an invisible EditText it will remain as you made it.

Ben Garney
  • 11
  • 1