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?
6 Answers
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.

- 2,914
- 1
- 22
- 35
-
23For unlocking it u use mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); – support_ms Mar 17 '15 at 05:06
-
8This 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. – Salman Khakwani Aug 17 '15 at 18:27
-
I am having that same issue. – Reid Mac Nov 06 '15 at 15:36
-
@SalmanKhakwani and reid-mac check out Vit-Vetal's answer below. – Fatima Feb 24 '18 at 11:17
-
It also disabled the close gesture and the back button close action. – Dan Meng Jul 27 '18 at 02:50
-
1think 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 Sep 04 '18 at 06:40
-
Currently there is a bug with the method. I have reported it here: https://issuetracker.google.com/issues/136738274 – i_tanova Jul 04 '19 at 14:41
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;

- 4,539
- 5
- 33
- 55

- 4,933
- 4
- 41
- 55
-
And this now appears on dev docs: http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html#LOCK_MODE_LOCKED_CLOSED – greg7gkb Oct 15 '14 at 22:34
-
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

- 705
- 8
- 23
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.

- 2,431
- 3
- 19
- 35
-
1You are right. The reason is described well here by @A--C https://stackoverflow.com/questions/18413116/why-i-cant-lock-drawerlayout-with-layout-gravity/18413276#18413276 – Fatima Feb 24 '18 at 11:16
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://stackoverflow.com/a/52160351/377320

- 3,088
- 28
- 39
-
I don't know but it is not working, I can still open it with a swipe gesture. – htafoya Jan 30 '23 at 01:57
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.

- 131
- 7