Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show software keyboard without EditText

My goal is to show/hide on-screen software keyboard on some event and intercept input from that keyboard.

I found out that soft keyboard can be shown for some View class descendants, but I don't need any visual representation of the text edit widget on screen, just the ability to programmatically show/hide soft keyboard with input interception.

What is the best way to achieve this?

like image 398
Nekuromento Avatar asked Feb 16 '12 12:02

Nekuromento


People also ask

How do I get the keyboard to show in Edittext?

edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears. This open the keyboard at Activity creation.

How to hide editText keyboard Android?

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.


2 Answers

Even if this question was asked almost a year ago it didn't have an accepted and fully helpful answer and since I ran into the same problem myself I though I'd share my solution:

As Vikram pointed out this is the way to show the soft input:

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

BUT you must also set your view as focusable and focusable in touch mode:

myView.setFocusable(true);
myView.setFocusableInTouchMode(true);

or in your view XML:

android:focusable = "true"
android:focusableInTouchMode = "true"
like image 105
britzl Avatar answered Oct 02 '22 14:10

britzl


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

and to hide:

((InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(findViewById(R.id.YOUR_VIEW).getWindowToken(), 0);
like image 42
Vikram Avatar answered Oct 02 '22 13:10

Vikram