How do I close the keyboard when the navigation drawer opens?
Asked Answered
E

8

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?

Estas answered 25/8, 2013 at 14:46 Comment(0)
R
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
);
Rawhide answered 14/1, 2014 at 10:56 Comment(2)
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"Ive
This is what finally got things working for me. Thank you.Estas
M
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();
Methanol answered 22/8, 2016 at 21:4 Comment(2)
Sure, add this code to your onCreate method and whenever you slide to open the drawer/ the drawer is opened the keyboard hides also, don't forget to declare your toolbar in your onCreate beforeMethanol
For me, it was enough to override onDrawerOpened().Qianaqibla
S
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();
        }
}
System answered 10/1, 2014 at 23:51 Comment(0)
K
3

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

Knurl answered 25/8, 2013 at 15:41 Comment(7)
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.Estas
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... });Knurl
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.Estas
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. :/Estas
Just write 'MyActivity activity = (MyActivity) getActivity; ' and then 'activity.setTheEditText(someEditText)' then your activity will know the edittextKnurl
Thanks. I'm give that a try and let you know what happens.Estas
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.Estas
R
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);
            }
        });
Reluctivity answered 9/2, 2016 at 23:32 Comment(0)
C
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);

Constantan answered 25/8, 2013 at 15:47 Comment(1)
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.Estas
E
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;

    }
Entwine answered 18/2, 2015 at 8:59 Comment(0)
H
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);
}
Hi answered 5/7, 2018 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.