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
  • 38,002
  • 70
  • 184
  • 260
  • 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

2 Answers2

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
  • 2,036
  • 6
  • 33
  • 48
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
          }
        }
    });