Disable gesture listener on DrawerLayout
Asked Answered
D

6

73

How can I disable the gesture recognition for the DrawerLayout? (swipe left to right) and only accept the close gesture (right to left) and open the drawer just with the home button?

Dewar answered 10/6, 2013 at 14:14 Comment(0)
A
177

This worked for me:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

You can expand the drawer by tapping the Home button, and can use right-to-left swipe gesture to dismiss it. However, left-to-right swipe is no longer triggered.

Altheta answered 18/7, 2013 at 4:21 Comment(7)
For unlocking it u use mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);Purcell
This is not working in my case, I always get a drawer on swipe gesture even if I set the Drawer Layout with the above Solution.Eau
I am having that same issue.Graf
@SalmanKhakwani and reid-mac check out Vit-Vetal's answer below.Lindsaylindsey
It also disabled the close gesture and the back button close action.Thoer
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.Cheer
Currently there is a bug with the method. I have reported it here: issuetracker.google.com/issues/136738274Medico
G
23

For setDrawerLockMode(), this is in the code but not on the Android developer docs:

/**
 * The drawer is unlocked.
 */
public static final int LOCK_MODE_UNLOCKED = 0;

/**
 * The drawer is locked closed. The user may not open it, though
 * the app may open it programmatically.
 */
public static final int LOCK_MODE_LOCKED_CLOSED = 1;

/**
 * The drawer is locked open. The user may not close it, though the app
 * may close it programmatically.
 */
public static final int LOCK_MODE_LOCKED_OPEN = 2;
Glorygloryofthesnow answered 8/4, 2014 at 17:48 Comment(2)
And this now appears on dev docs: developer.android.com/reference/android/support/v4/widget/…Glorygloryofthesnow
It doesn't seem to work, I can still drag to open it.Moiety
C
3

To disable the DrawerLayout gesture recognition use:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);

Then, to enable right to left swipe, check this resource: http://android-journey.blogspot.com/2010/01/android-gestures.html

Chuchuah answered 12/7, 2013 at 7:26 Comment(0)
U
3

Look like I found bug. For example if set:

android:layout_gravity="right"

or

android:layout_gravity="left"

for drawer content and use .setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) all will be fine.

But in case when android:layout_gravity="left|center_vertical" or something like it LOCK_MODE_LOCKED_CLOSED will not work.

Unpile answered 15/12, 2017 at 11:21 Comment(1)
You are right. The reason is described well here by @A--C #18413616Lindsaylindsey
C
3

The LOCK_MODE_LOCKED_CLOSED nowadays completely prevents the nav menu from showing, even via the hamburger menu (which may be unwanted). The following worked for me: https://mcmap.net/q/118547/-disable-the-swipe-gesture-that-opens-the-navigation-drawer-in-android

Cheer answered 4/9, 2018 at 6:50 Comment(1)
I don't know but it is not working, I can still open it with a swipe gesture.Moiety
S
2

This worked for me:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

You can expand the drawer by tapping the Hamburger icon or button from which you have to trigger. However, left-to-right swipe is no longer triggered.

Sniffle answered 12/5, 2018 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.