Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close the keyboard when the navigation drawer opens?

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?

like image 881
blazingwolf Avatar asked Aug 25 '13 14:08

blazingwolf


People also ask

How do I close open keyboard?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.


2 Answers

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();
like image 122
Gilad Brunfman Avatar answered Oct 21 '22 10:10

Gilad Brunfman


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);
            }
        });
like image 43
llBuckshotll Avatar answered Oct 21 '22 11:10

llBuckshotll