views:

17680

answers:

5

I'm having an EditText and a Button in my layout. After writing inside the edit field and clicking on the Button, I want to hide the virtual keyboard. I guess there should be a simple, one- or two-liner to make this happen. Where can I find an example of it?

+49  A: 

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

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

Reto Meier
Perfect, just what i was looking for.
PHP_Jedi
Thanks this seems to work great if using 0 as the second parameter. But if I use InputMethodManager.HIDE_IMPLICIT_ONLY the keyboard is never hidden (although i'm not holding down menu). Any hints?
Roflcoptr
+3  A: 

Please try this below code in oncreate()

EditText edtView=(EditText)findViewById(R.id.editTextConvertValue);
edtView.setInputType(0);
Jeyavel
This method works as a means of getting around the "can't hide the soft keyboard" bug in 2.0 and 2.1 as described in http://code.google.com/p/android/issues/detail?id=7115 ... the hideSoftInputFromWindow method listed above did not work when I tried it, but editView.setInputType(0) did.
Spike Williams
This is legit per Javadoc (not a hack) though I would rewrite the method as `editView.setInputType(InputType.TYPE_NULL);`
DroidIn.net
+10  A: 

Also useful for hiding the soft keyboard is:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

This can be used to suppress the keyboard until the user actually touched the edittext view.

Garnet Ulrich
This totally worked for me!
DroidIn.net
+3  A: 

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.

   tabHost.setOnTabChangedListener(new OnTabChangeListener()
        {
        public void onTabChanged(String tabId)
            {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
            }
        }
mckoss
A: 

I tried all the above steps but nothing worked for me.What I am trying to do is close soft keyboard & as well finish the activity.( http://stackoverflow.com/questions/3940127/intercept-back-button-from-soft-keyboard/3994485).

I found that it is not the onKeyDown action that is invoked when clicking Back when Soft Keyboard is On. Could you please guide me what action is performed at the mentioned moment.

100rabh