I have an EditText
and a Button
in my layout.
After writing in the edit field and clicking on the Button
, I want to hide the virtual keyboard. I assume that this is a simple piece of code, but where can I find an example of it?
I have an EditText
and a Button
in my layout.
After writing in the edit field and clicking on the Button
, I want to hide the virtual keyboard. I assume that this is a simple piece of code, but where can I find an example of it?
Just use this optimized code in your activity:
from so searching, here I found an answer that works for me
Meier's solution works for me too. In my case the top level of my App is a tabHost and I want to hide the keyword when switching tabs - I get the window token from the tabHost View.
I'm using a custom keyboard to input an Hex number so I can't have the IMM keyboard show up...
In v3.2.4_r1
setSoftInputShownOnFocus(boolean show)
was added to control weather or not to display the keyboard when a TextView gets focus, but its still hidden so reflection must be used:For older versions, I got very good results (but far from perfect) with a
OnGlobalLayoutListener
, added with the aid of aViewTreeObserver
from my root view and then checking if the keyboard is shown like this:This last solution may show the keyboard for a split second and messes with the selection handles.
When in the keyboard enters full screen, onGlobalLayout isn't called. To avoid that, use TextView#setImeOptions(int) or in the TextView XML declaration:
Update: Just found what dialogs use to never show the keyboard and works in all versions:
Here's how you do it in Mono for Android (AKA MonoDroid)
You can force Android to hide the virtual keyboard using the InputMethodManager, calling
hideSoftInputFromWindow
, passing in the token of the window containing your focused view.This will force the keyboard to be hidden in all situations. In some cases you will want to pass in
InputMethodManager.HIDE_IMPLICIT_ONLY
as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).Note: If you want to do this in Kotlin, use:
context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager