androidmenunavigation-drawerslidingdrawer

How to keep closed the Sliding Menu even when user slides to open it?


In my application, I have some screens where I don't want the drawer menu to open when the user slides. How can I do that? The app uses only one activity and the rest are fragments.


Solution

  • How to keep closed the Sliding Menu even when user slides to open it?

    Use below method in your Activity and call it as your need.

     public void enableViews(boolean enable) {
           if (enable) {
                //You may not want to open the drawer on swipe from the left in this case
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                // Remove hamburger
                toggle.setDrawerIndicatorEnabled(false);
                // Show back button
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                // when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
                // clicks are disabled i.e. the UP button will not work.
                // We need to add a listener, as in below, so DrawerToggle will forward
                // click events to this listener.
                toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Doesn't have to be onBackPressed
                        onBackPressed();
                    }
                });
            } else {
                //You must regain the power of swipe for the drawer.
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                // Remove back button
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    
                // Show hamburger
                toggle.setDrawerIndicatorEnabled(true);
                // Remove the/any drawer toggle listener
                toggle.setToolbarNavigationClickListener(null);
            }
        }
    

    Note:- Call enableViews(true) for locking and enableViews(false) for unlocking