Search⌘ K

Drawer Layout

Explore how to implement a drawer layout in your Android app to provide smooth navigation across multiple destinations. Understand how to create menu and header layouts, connect them with the drawer container, and manage navigation events in an activity.

We'll cover the following...

Drawer layout

The drawer layout is a menu container that contains navigation destinations. A user can slide it open and close after use. They’re normally recommended for applications with more than five fragments. Although our application has only three destinations, let’s learn how to create and use it.

Let’s go through the code.

Layouts

XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/github_repo"
android:title="@string/github_repository"
android:icon="@drawable/git"/>
<item
android:id="@+id/rate_app"
android:title="@string/rate_this_app"
android:icon="@drawable/star"/>
<item
android:id="@+id/developer"
android:title="@string/about_developer"
android:icon="@drawable/programmer"/>
</menu>

In the code snippet above, we have a menu layout called navigation_drawer_menu.xml that contains different options that a user can click to navigate to different destinations, like settings or a destination to rate the app.

XML
<com.google.android.material.navigation.NavigationView
android:id="@+id/navMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/nav_header_layout"
app:menu="@menu/navigation_drawer_menu"
app:itemMaxLines="2"
android:layout_gravity="start"
app:itemTextColor="@color/colorWhite"
android:backgroundTint="@color/colorBlack"
android:fitsSystemWindows="true"/>

The above code ...