android – navigation drawer hide keyboard when onDrawerOpened-ThrowExceptions

Exception or error:

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.

How to solve:

please use this line of code before ope/closing the slide drawer

InputMethodManager inputMethodManager = (InputMethodManager)  this.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(this.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);
        }
    };

Happy coding…

###

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

    }
});  

###

the problem is that getWindowToken() must be called from the View that is currently “holding” the keyboard. It is very annoying, I agree with you, but that is how it works.

For example let’s say EditText mEditText is the object currently in focus receiving the keyboard keystrokes. So then your code would be:

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

hope it helps.

###

This is the simple best solution i’ve come up so far.All you gotta do is to create a InputMethodManager object. Add a listener to the DrawerLayout object and finally
add the line of code in the following methods inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0); and your good to go.

InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);

    drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View view, float v) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
        }
        @Override
        public void onDrawerOpened(@NonNull View view) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
        }

        @Override
        public void onDrawerClosed(@NonNull View view) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
        }

        @Override
        public void onDrawerStateChanged(int i) {
        }
    });

###

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.

###

**If You Used the toggle with drawer then add onDrawerStateChanged manually in your onCreate method **

DrawerLayout drawer = findViewById(R.id.drawer_layout);
//ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, appToolBar, R.string.Open_Drawer, R.string.Close_Drawer);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, appToolBar, 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(Activity.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(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
   */     }

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

        }
    };




    drawer.addDrawerListener(toggle);

    toggle.syncState();

Leave a Reply

Your email address will not be published. Required fields are marked *