Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigation drawer hide keyboard when onDrawerOpened

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.

like image 449
SooCheng Koh Avatar asked Jul 07 '13 20:07

SooCheng Koh


People also ask

How do I customize my navigation drawer?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.

How do I hide the buttons on my keyboard?

To hide keyboard, use the following code. InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager. hideSoftInputFromWindow(v. getApplicationWindowToken(),0);


7 Answers

Use this line of code before opening/closing the slide drawer:

InputMethodManager inputMethodManager = (InputMethodManager)  this.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
like image 170
Ramz Avatar answered Oct 12 '22 19:10

Ramz


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);
        }
    };
like image 26
Silambarasan Poonguti Avatar answered Oct 12 '22 20:10

Silambarasan Poonguti


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

    }
});  
like image 44
Karan sharma Avatar answered Oct 12 '22 19:10

Karan sharma


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.

like image 27
Afrig Aminuddin Avatar answered Oct 12 '22 18:10

Afrig Aminuddin


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) {
        }
    });
like image 23
Nelson Katale Avatar answered Oct 12 '22 19:10

Nelson Katale


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);
like image 20
Budius Avatar answered Oct 12 '22 20:10

Budius


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();
like image 34
Amit Chavan Avatar answered Oct 12 '22 19:10

Amit Chavan