disable the swipe gesture that opens the navigation drawer in android
Solution 1
You should use:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
It worked for me, the swipe to open the drawer was disabled.
If it still won't work, check out the answer provided here.
Solution 2
for locking you can do this:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
and for unlock :
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
Solution 3
Add gravity value too when using setDrawerLockMode();
Do this :
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
This should work like a charm
Solution 4
The answer to disable swiping is the correct one. I think LOCK_MODE_LOCKED_CLOSED worked in Compat 24.x, but the functionality has been changed in newer compat libraries and LOCK_MODE_LOCKED_CLOSED now completely prevents the nav menu from showing, even via using the hamburger menu.
The following class works for me (Kotlin):
class MyDrawerLayout(ctx: Context) : DrawerLayout(ctx) {
var isSwipeOpenEnabled: Boolean = true
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
if (!isSwipeOpenEnabled && !isDrawerVisible(Gravity.START)) {
return false
}
return super.onInterceptTouchEvent(ev)
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(ev: MotionEvent): Boolean {
if (!isSwipeOpenEnabled && !isDrawerVisible(Gravity.START)) {
return false
}
return super.onTouchEvent(ev)
}
}
Solution 5
To disable swiping, override onInterceptTouchEvent
and onTouchEvent
on DrawerLayout
and have them return false.
Related videos on Youtube
user1627990
Updated on July 28, 2021Comments
-
user1627990 almost 3 years
I've been following the navigation drawer guide by Google and I'd like to add it to an Activity with tabs and gestures.
I'd like to disable the gesture to open the navigation drawer, does anyone have any idea how to do this?
-
thanhnh almost 11 yearsAre you sure? I tried but it was diabled open and close, both. I were testing on android 2.3x with ActionBarSherlock framework.
-
Tran Hieu over 10 yearsI'm sure because I've used it in my code. But I did not use with ActionBarSherlock. I used with ActionBar only.
-
egfconnor over 10 yearsTo clarify: DrawerLayout.LOCK_MODE_LOCKED_OPEN locks the drawer to the open state so the user can't hide it. DrawerLayout.LOCK_MODE_LOCKED_CLOSED locks the drawer to the closed state so the user can't open it. Lastly, DrawerLayout.LOCK_MODE_UNLOCKED unlocks the drawer so it can be open or closed.
-
Igoussam about 10 yearsI have corrected the answer to LOCK_MODE_LOCKED_CLOSED.
-
Steve Liddle almost 10 yearsIf you don't already have
mDrawerLayout
, set it as follows:mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
-
Azurespot about 9 yearsBut how about if we want to control the open/close with the app icon only, and not swiping? Is there a way to do that? I am curious because I'm trying to implement the new
ToolBar
(API 21) and use theSlidingTabLayout
feature it has, which is conflicting with my Nav. drawer swiping. So I'd rather disable the Nav. drawer slide in favor of theToolBar
sliding feature. -
Dylan Vander Berg over 8 yearsTry overriding
onDrawerSlide()
in yourDrawerListener
and not calling the super method. -
Mattia Ruggiero over 7 yearsThanks for clarifying the accepted answer with the LOCK_MODE_UNLOCKED flag
-
TheWhiteLlama about 7 yearsI think this is better than the accepted answer since it also introduces the opposing method
-
Timo Albert over 6 yearsAlso if you have two drawers on both sides you can lock one by getting the view of the NavigationView and doing
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, navigationView)
-
Sanjeev Sangral about 6 yearsmDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); lock the both Drawer and drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END); represent which Drawer you want to lock thanks man it works for me and save my time.
-
HelloWorld about 6 yearsthis disables the entire drawer (e.g. icon does not work anymore), not just swiping, hence did not answer the question -- which specifically referred to swiping
-
Martin Vysny over 5 yearsI think LOCK_MODE_LOCKED_CLOSED worked in Compat 24.x, but the functionality has been changed in newer compat libraries and LOCK_MODE_LOCKED_CLOSED now completely prevents the nav menu from showing, even via using the hamburger menu. Other solution is now needed.
-
Martin Vysny over 5 yearsThis is the correct answer. Let me elaborate on this a bit more in the answer.
-
ProjectJourneyman over 5 yearsIn the current sources, it looks like 3 for left and 5 for right will work as expected, but GravityCompat.END does not.
-
Syed Hussain Mehdi about 5 yearsIf I used mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); for locking it closed the drawer so, for locking i used mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
-
i_tanova almost 5 yearsCurrently there is a bug with the method. I have reported it here: issuetracker.google.com/issues/136738274
-
Michael Tolsma over 3 years
error: cannot find symbol mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);