Question

I have added a navigation drawer to my application. So far everything else works well but I am having an issue where when the navigation drawer opens the keyboard is not closed. The navigation drawer is the main activity and then each page opened from the drawer is a fragment.

I have tried adding the following to each one of my EditText areas in the fragments. However, this is not closing anything.

InputMethodManager imm1 = (InputMethodManager)getActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
        imm1.hideSoftInputFromWindow(input1.getWindowToken(), 0);

I have also tried placing that code in the main activity but have been unsuccessful there as well. Any ideas on what I can do differently to get this working?

Was it helpful?

Solution

To hide an open keyboard while opening or closing the navigation drawer please override method onDrawerSlide in onDrawerListner and add below line

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

OTHER TIPS

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle  actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
       }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    };
    drawer.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

This worked very well for me:

private ActionBarDrawerToggle aDrawerToggle;
private DrawerLayout aDrawerLayout;

I'm using this code in a void after creating the class:

        aDrawerToggle = new ActionBarDrawerToggle(getActivity(), 
        aDrawerLayout, 
        R.drawable.main_icon, 
        R.string.drawer_open, 
        R.string.drawer_close){

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) {
                return;
            }

            InputMethodManager inputMethodManager = (InputMethodManager)getActivity().getSystemService(
                    Context.INPUT_METHOD_SERVICE);


            //inputSearch is my EditText
            inputMethodManager.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);

            getActivity().supportInvalidateOptionsMenu();
        }
}

Add a DrawerListener to your DrawerLayout. Then you can use the code above to close the keyboard in the onDrawerOpened() method

In onCreate:

DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
            }

            @Override
            public void onDrawerOpened(View drawerView) {
            }

            @Override
            public void onDrawerClosed(View drawerView) {
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.
                        INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
        });

Try this code, I use it in my apps and for example if I open a dialog that contains a EditText I set this code in on create.Hope this helps

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

This does not solve issue.
Correct solution is: Override below method on your main activity:

      public boolean dispatchTouchEvent(MotionEvent ev)
    {

        View view = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(ev);
if(drawer_open) {

//DO nothing if slider open } else { if (view instanceof EditText) { View w = getCurrentFocus(); int scrcoords[] = new int[2]; w.getLocationOnScreen(scrcoords); float x = ev.getRawX() + w.getLeft() - scrcoords[0]; float y = ev.getRawY() + w.getTop() - scrcoords[1];

        if (ev.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }
    }

        return ret;

    }

Since, drawer.setDrawerListener(toggle) is deprecated. Use drawer.addDrawerListener(toggle) as mentioned below

private void initDrawerLayout() {

    drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle
            (this, drawer, toolbar,
                    R.string.navigation_drawer_open,
                    R.string.navigation_drawer_close)
    {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    };
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    drawer.closeDrawer(GravityCompat.START);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top