A navigation drawer helps users move between different parts of your app easily. It slides from the side and shows menu options.
Navigation drawer in Android Kotlin
class MainActivity : AppCompatActivity() { private lateinit var toggle: ActionBarDrawerToggle override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val drawerLayout = findViewById<DrawerLayout>(R.id.drawer_layout) val navView = findViewById<NavigationView>(R.id.nav_view) // Setup toggle button to open/close drawer toggle = ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close) drawerLayout.addDrawerListener(toggle) toggle.syncState() supportActionBar?.setDisplayHomeAsUpEnabled(true) navView.setNavigationItemSelectedListener { menuItem -> // Handle menu item clicks here drawerLayout.closeDrawer(GravityCompat.START) true } } override fun onOptionsItemSelected(item: MenuItem): Boolean { if (toggle.onOptionsItemSelected(item)) { return true } return super.onOptionsItemSelected(item) } }
The drawer layout wraps your main content and the navigation menu.
ActionBarDrawerToggle adds the hamburger icon to open/close the drawer.
<androidx.drawerlayout.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Main content --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- Navigation drawer --> <com.google.android.material.navigation.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/drawer_menu" /> </androidx.drawerlayout.widget.DrawerLayout>
navView.setNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.nav_home -> {
// Navigate to home
}
R.id.nav_settings -> {
// Navigate to settings
}
}
drawerLayout.closeDrawer(GravityCompat.START)
true
}This is a simple app with a navigation drawer. When you tap the hamburger icon, the drawer slides in. Clicking menu items shows a small message and closes the drawer.
class MainActivity : AppCompatActivity() { private lateinit var drawerLayout: DrawerLayout private lateinit var toggle: ActionBarDrawerToggle override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) drawerLayout = findViewById(R.id.drawer_layout) val navView: NavigationView = findViewById(R.id.nav_view) toggle = ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close) drawerLayout.addDrawerListener(toggle) toggle.syncState() supportActionBar?.setDisplayHomeAsUpEnabled(true) navView.setNavigationItemSelectedListener { menuItem -> when (menuItem.itemId) { R.id.nav_home -> Toast.makeText(this, "Home clicked", Toast.LENGTH_SHORT).show() R.id.nav_settings -> Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show() } drawerLayout.closeDrawer(GravityCompat.START) true } } override fun onOptionsItemSelected(item: MenuItem): Boolean { if (toggle.onOptionsItemSelected(item)) { return true } return super.onOptionsItemSelected(item) } }
Remember to add the drawer menu items in a menu resource XML file.
Use android:layout_gravity="start" to make the drawer slide from the left side.
Test on different screen sizes to ensure the drawer works well everywhere.
A navigation drawer helps users switch app sections easily.
It slides from the side and hides when not needed.
Use DrawerLayout and NavigationView with ActionBarDrawerToggle in Kotlin.
Practice
Navigation Drawer in an Android app?Solution
Step 1: Understand the role of Navigation Drawer
A Navigation Drawer is a UI panel that slides from the side to show app sections.Step 2: Match purpose with options
Only To let users switch between different sections of the app easily describes switching app sections, which is the main use of Navigation Drawer.Final Answer:
To let users switch between different sections of the app easily -> Option BQuick Check:
Navigation Drawer = Switch app sections [OK]
- Confusing drawer with notifications panel
- Thinking it shows pop-up messages
- Assuming it controls media playback
DrawerLayout and ActionBarDrawerToggle in an Activity?Solution
Step 1: Check ActionBarDrawerToggle constructor parameters
The correct order is (Activity, DrawerLayout, Toolbar, openDescRes, closeDescRes).Step 2: Verify code matches this order
val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) drawerLayout.addDrawerListener(drawerToggle) drawerToggle.syncState() matches the correct parameter order and calls addDrawerListener and syncState properly.Final Answer:
val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) drawerLayout.addDrawerListener(drawerToggle) drawerToggle.syncState() -> Option CQuick Check:
Correct constructor order = val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) drawerLayout.addDrawerListener(drawerToggle) drawerToggle.syncState() [OK]
- Swapping DrawerLayout and Toolbar parameters
- Missing open/close string resource IDs
- Not calling addDrawerListener or syncState
val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) drawerLayout.addDrawerListener(drawerToggle) drawerToggle.syncState()
Solution
Step 1: Understand ActionBarDrawerToggle behavior
It links the hamburger icon to open/close the drawer when tapped.Step 2: Check code completeness
Code adds drawerToggle as listener and calls syncState, so toggle works correctly.Final Answer:
The navigation drawer slides in from the side -> Option DQuick Check:
Hamburger tap = Drawer slides in [OK]
- Thinking syncState is optional
- Assuming no effect without extra code
- Confusing drawer toggle with toolbar title change
val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) drawerToggle.syncState()
Solution
Step 1: Review drawer toggle setup steps
After creating drawerToggle, you must add it as a listener to drawerLayout.Step 2: Identify missing method call
The code calls syncState but does not call drawerLayout.addDrawerListener(drawerToggle), so toggle won't work.Final Answer:
Missing call to drawerLayout.addDrawerListener(drawerToggle) -> Option AQuick Check:
Must add drawerToggle as listener [OK]
- Forgetting addDrawerListener call
- Mixing parameter order in constructor
- Skipping setSupportActionBar(toolbar) setup
Solution
Step 1: Understand standard navigation drawer menu setup
Menus are usually defined in XML and linked to NavigationView for easy management.Step 2: Handle item clicks with setNavigationItemSelectedListener
This listener handles user taps on menu items properly.Final Answer:
Define menu in XML, set NavigationView's menu, then use setNavigationItemSelectedListener to handle clicks -> Option AQuick Check:
XML menu + listener = Define menu in XML, set NavigationView's menu, then use setNavigationItemSelectedListener to handle clicks [OK]
- Trying to handle drawer menu with onOptionsItemSelected
- Using RecyclerView unnecessarily for simple menus
- Placing buttons directly inside DrawerLayout instead of NavigationView
Ready to prove it?
Pass the test and this pattern moves unknown
