navigation drawer hide keyboard when onDrawerOpened

I create my own Helper class to show or hide keyboard.

Here it is...

public static class Helper {

    public static void showKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
        }
    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && activity.getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }

}

Call Helper.showKeyboard(this) to show the keyboard. Call Helper.hideKeyboard(this) to hide the keyboard. this refers to Activity.


Set drawer listener is already deprecated you can detect navigation drawer state change using mDrawerLayout.addDrawerListener() and close keyboard onDrawerStateChange

mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        //Called when a drawer's position changes.

    }

    @Override
    public void onDrawerOpened(View drawerView) {
        //Called when a drawer has settled in a completely open state.
        //The drawer is interactive at this point.
        // If you have 2 drawers (left and right) you can distinguish 
        // them by using id of the drawerView. int id = drawerView.getId(); 
        // id will be your layout's id: for example R.id.left_drawer            
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        // Called when a drawer has settled in a completely closed state.
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        // Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
        InputMethodManager inputMethodManager = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

    }
});  

Try this...

@Override
protected void onCreate(Bundle savedInstanceState) {
    ......
   
    //Initialize
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
   
    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();

}

DrawerListerner:

 ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
            super.onDrawerOpened(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    };

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