Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use the soft keyboard without an EditText?

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!

like image 612
ang Avatar asked Jan 02 '11 17:01

ang


People also ask

How do I hide the soft keyboard on Android after clicking outside EditText?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I hide soft keyboard when EditText is focused?

setShowSoftInputOnFocus(false); to disable the software keyboard showing when the EditText is touched. the hideKeyboard(this); call in OnCreate to forcible hide the software keyboard.

How do I get rid of soft keyboard on Android?

Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.


Video Answer


2 Answers

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.

like image 68
miha Avatar answered Sep 28 '22 09:09

miha


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()

like image 21
Terel Avatar answered Sep 28 '22 09:09

Terel