9

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?

blazingwolf
  • 251
  • 3
  • 8

8 Answers8

15

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
);
fejese
  • 4,601
  • 4
  • 29
  • 36
Ramz
  • 7,116
  • 6
  • 63
  • 88
  • when use this,the fragment view will lost focus,and then the focus will in the fragment first focus able view, is there a way to avoid that. the other problem is in `onDrawerSlide` method invoke,this random show a view(in my case,is a button for submit function) will up to screen which like use `android:windowSoftInputMode="adjustResize"` – chinaanihchen Nov 20 '15 at 09:27
  • This is what finally got things working for me. Thank you. – blazingwolf May 07 '16 at 17:30
11
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();
Gilad Brunfman
  • 3,452
  • 1
  • 29
  • 29
4

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();
        }
}
Nikolay Hristov
  • 1,602
  • 1
  • 15
  • 22
3

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

dumazy
  • 13,857
  • 12
  • 66
  • 113
  • Thank you for the answer, I appreciate you taking the time. I've tried to set this up on my own and I don't understand the exact implementation. Can you show me a code snippet of how it should be done? My searches have led me nowhere so far. I've been using the Google version of the drawer. – blazingwolf Aug 25 '13 at 21:18
  • 1
    Are you using the DrawerLayout of the support library? If you are, just give the DrawerLayout in your xml file an id and then get an instance of DrawerLayout in your Activity by using "DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.myDrawerLayout); " for example. Then you can implement it like this: mDrawerLayout.setDrawerListener(new DrawerListener(){ ... //override methods here... }); – dumazy Aug 25 '13 at 21:32
  • Yes, I am using the DrawerLayout in the support library. However, I apparently have my "Dunce" cap on as every time I try to implement the code you mentioned I get errors. The findViewById is already set to the DrawerLayout id so I try to set up the new listener and that is when the errors occur and I also get an error on the new listener (says new is an invalid type). I imagine it's just something I'm doing wrong but darn if I can figure it out right now. Thanks for all of your help. – blazingwolf Aug 26 '13 at 03:33
  • OK. Finally got the DrawerListener set up with out errors. :D Still having issues with hiding the keyboard though. My main activity doesn't have any edittext fields in it only the fragments do. So I am unable to say "(EditText.getWindowToken,0);.Once I remove the EditText portion I get an error that says getWindowToken is undefined. :/ – blazingwolf Aug 26 '13 at 04:15
  • Just write 'MyActivity activity = (MyActivity) getActivity; ' and then 'activity.setTheEditText(someEditText)' then your activity will know the edittext – dumazy Aug 26 '13 at 05:21
  • Thanks. I'm give that a try and let you know what happens. – blazingwolf Aug 26 '13 at 17:00
  • I'm obviously not smart enough to figure this out yet as nothing I have done works to close the keyboard. I have released though that the list is scrollable so that works out as a good work around. If you have any better ideas or code to show me I'm open to all suggestions. Thanks for the current input. – blazingwolf Dec 11 '13 at 03:42
2

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);
            }
        });
llBuckshotll
  • 119
  • 1
  • 9
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);

MSA
  • 2,502
  • 2
  • 22
  • 35
  • Thanks for the answer, I appreciate you taking the time to answer. This doesn't do anything for me when I add it to my app though so I'm assuming I'm doing something wrong. – blazingwolf Aug 25 '13 at 21:21
0
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;

    }
silwalprabin
  • 637
  • 7
  • 20
0

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);
}
Ranjith
  • 21
  • 5