Android Development
Swipe Gesture
Navigation Drawer
UI Customization
Mobile App Development

disable the swipe gesture that opens the navigation drawer in android

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Android DrawerLayout opens by edge swipe by default, which is useful for many apps but can conflict with custom gestures, maps, or horizontally scrolling content. You can disable swipe opening while still allowing programmatic open and close actions. The key is drawer lock mode.

Disable Swipe with Lock Mode

Set drawer lock mode to LOCK_MODE_LOCKED_CLOSED for the start drawer. This prevents gesture open attempts.

kotlin
1val drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
2drawer.setDrawerLockMode(
3    DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
4    GravityCompat.START
5)

You can still open it manually in code when needed.

kotlin
drawer.openDrawer(GravityCompat.START)

This pattern is useful when the menu should open only from a toolbar button.

Integrating with Toolbar Button

Pair lock mode with a controlled click action.

kotlin
binding.menuButton.setOnClickListener {
    binding.drawerLayout.openDrawer(GravityCompat.START)
}

If you use ActionBarDrawerToggle, keep it synchronized but disable gesture driven open with lock mode.

kotlin
1val toggle = ActionBarDrawerToggle(
2    this,
3    binding.drawerLayout,
4    binding.toolbar,
5    R.string.drawer_open,
6    R.string.drawer_close
7)
8binding.drawerLayout.addDrawerListener(toggle)
9toggle.syncState()

Temporarily Enabling Swipe

Some flows need dynamic behavior, such as locking during onboarding and unlocking later.

kotlin
1fun setDrawerSwipeEnabled(enabled: Boolean) {
2    val mode = if (enabled) {
3        DrawerLayout.LOCK_MODE_UNLOCKED
4    } else {
5        DrawerLayout.LOCK_MODE_LOCKED_CLOSED
6    }
7    binding.drawerLayout.setDrawerLockMode(mode, GravityCompat.START)
8}

This keeps UX flexible while preventing accidental drawer reveals in sensitive screens.

Handling Back Press with Locked Drawer

If drawer is locked closed, back press usually should not attempt drawer close first. If it is open programmatically, close it before leaving screen.

kotlin
1override fun onBackPressed() {
2    val drawer = binding.drawerLayout
3    if (drawer.isDrawerOpen(GravityCompat.START)) {
4        drawer.closeDrawer(GravityCompat.START)
5    } else {
6        super.onBackPressed()
7    }
8}

In modern apps, prefer OnBackPressedDispatcher for lifecycle aware handling.

When using Navigation UI helpers, set up drawer once, then apply lock mode based on destination.

kotlin
1navController.addOnDestinationChangedListener { _, destination, _ ->
2    val disableSwipe = destination.id == R.id.fullscreenMapFragment
3    val mode = if (disableSwipe) {
4        DrawerLayout.LOCK_MODE_LOCKED_CLOSED
5    } else {
6        DrawerLayout.LOCK_MODE_UNLOCKED
7    }
8    binding.drawerLayout.setDrawerLockMode(mode, GravityCompat.START)
9}

This avoids global settings that break expected behavior in other sections.

Testing Gesture Lock Behavior

Verify behavior on physical devices and emulators because edge gesture sensitivity can vary.

kotlin
1@Test
2fun drawerDoesNotOpenBySwipeOnMapScreen() {
3    // Navigate to destination with locked drawer
4    // Perform edge swipe
5    // Assert drawer remains closed
6}

Add these checks to UI test suites if drawer state is part of navigation contract.

Accessibility and Large Screen Considerations

On tablets and desktop mode layouts, a permanently visible navigation rail may replace drawer interactions entirely. If you keep DrawerLayout, ensure keyboard and accessibility actions can still open navigation when swipe is disabled. Always provide a clear visible control such as a menu button with content description for screen readers.

If your app supports gesture navigation from the operating system edge, test drawer lock behavior together with system back gestures on multiple Android versions. Device specific gesture areas can create interactions that look inconsistent unless they are tested early.

Document this behavior in your UX guidelines.

Common Pitfalls

  • Locking drawer globally and forgetting to unlock for screens where swipe is part of expected UX.
  • Calling setDrawerLockMode without specifying the correct gravity when both start and end drawers exist.
  • Assuming ActionBarDrawerToggle alone can disable gestures without lock mode.
  • Forgetting to handle back behavior when drawer can still be opened programmatically.
  • Applying lock logic before DrawerLayout is initialized and then debugging no effect calls.

Summary

  • Use LOCK_MODE_LOCKED_CLOSED to disable swipe opening on DrawerLayout.
  • Keep programmatic open and close actions available when needed.
  • Toggle lock mode dynamically for destination specific behavior.
  • Combine with proper back handling to keep navigation intuitive.
  • Validate drawer behavior with UI tests on real interaction paths.

Course illustration
Course illustration

All Rights Reserved.