4

Is there a way to hide the virtual keyboard once I click a button in android? The keyboard originally pops up when the user touches an edittext component; I'd like it to close once a button is pushed.

locoboy
  • 34,978
  • 62
  • 174
  • 253
  • possible duplicate of [How to close/hide the Android Soft Keyboard?](http://stackoverflow.com/questions/1109022/how-to-close-hide-the-android-soft-keyboard) – Aleadam May 08 '11 at 18:15

3 Answers3

19

To hide virtual keyboard try/use this

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(medtSearchQuery.getWindowToken(), 0);
secretformula
  • 6,256
  • 2
  • 30
  • 53
Eby
  • 3,059
  • 4
  • 20
  • 31
17

Best Practice for hiding keyboard:

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

It will automatically receive the current focus and will hide keyboard. Doesn't matter how many EditText views you have.

Sanjay Joshi
  • 1,542
  • 6
  • 27
  • 44
2

Use Below Code

    your_button_id.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           try  {
             InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
             imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
           } catch (Exception e) {
            // TODO: handle exception
          }
        }
    });