Search code examples
androidnavigation-drawer

navigation drawer hide keyboard when onDrawerOpened


I have a fragment with edittext in it. when i click the edittext, the keyboard show up. the problem is when i open the drawer, the drawer does not hide the keyboard. the keyboard is still showing even i switch to another fragment. How can i hide the keyboard when i open the drawer.

i try to put

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

and

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

both of it do not hide the keyboard.


Solution

  • Use this line of code before opening/closing the slide drawer:

    InputMethodManager inputMethodManager = (InputMethodManager)  this.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);