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.
Asked
Active
Viewed 63 times
2 Answers
1
Try this:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

Milad Bahmanabadi
- 946
- 11
- 27
1
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

sushildlh
- 8,986
- 4
- 33
- 77
-
I have multiple fragments.. I can write this code in every fragment. I need something that can be written in activity – Miki Jan 08 '19 at 12:12
-
you do this in activity and call it in activity whenever you opening fragments. – sushildlh Jan 08 '19 at 12:17
-
@Miki Check Edit Answer. – sushildlh Jan 08 '19 at 12:20