Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Navigation Drawer in Android

  • Difficulty Level : Basic
  • Last Updated : 26 Dec, 2020

The navigation drawer is the most common feature offered by android and the navigation drawer is a UI panel that shows your app’s main navigation menu. It is also one of the important UI elements, which provides actions preferable to the users like example changing user profile, changing settings of the application, etc. In this article, it has been discussed step by step to implement the navigation drawer in android. 

The navigation drawer slides in from the left and contains the navigation destinations for the app.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout. Have a look at the following image to get an idea about the Navigation drawer.

Navigation Drawer in Android

Steps to Implement Navigation Drawer in Android

Step 1: Create an Android Studio Project

Step 2: Adding a dependency to the project

  • In this discussion, we are going to use the Material design Navigation drawer.
  • So add the following Material design dependency to the app-level Gradle file.

implementation ‘’

  • Refer to the following image if unable to locate the app-level Gradle file invokes the dependency (under project hierarchy view).
  • After invoking the dependency click on the Sync Nowbutton. Make sure the system is connected to the network so that Android Studio downloads the required files.

Step 3: Creating a menu in the menu folder

  • Create the menu folder under the res folder. To implement the menu.
  • Refer to the following video to create the layout to implement the menu.
  • Invoke the following code in the navigation_menu.xml


<?xml version="1.0" encoding="utf-8"?>
        android:title="My Account" />
        android:title="Settings" />
        android:title="Logout" />

Step 4: Working with the activity_main.xml file

  • Invoke the following code in the activity_main.xml to set up the basic things required for the Navigation Drawer. 


<?xml version="1.0" encoding="utf-8"?>
<!--the root view must be the DrawerLayout-->
            android:textSize="18sp" />
    <!--this the navigation view which draws
        and shows the navigation drawer-->
    <!--include the menu created in the menu folder-->
        app:menu="@menu/navigation_menu" />

Output UI: 

  • One thing to be noticed is that the menu drawer icon is still not appeared on the action bar. We need to set the icon and its open-close functionality programmatically. 

Step 5: Include the Open Close strings in the string.xml 

  • Invoke the following code in the styles.xml file. 


    <string name="app_name">Navigation Drawer</string>
    <!--to toggle the open close button
        of the navigation drawer-->
    <string name="nav_open">Open</string>
    <string name="nav_close">Close</string>

Step 6: Working with the file

  • Invoke the following code in the file to show the menu icon on the action bar and implement the open-close functionality of the navigation drawer.
  • Comments are added inside the code for better understanding.


import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
    public DrawerLayout drawerLayout;
    public ActionBarDrawerToggle actionBarDrawerToggle;
    protected void onCreate(Bundle savedInstanceState) {
        // drawer layout instance to toggle the menu icon to open
        // drawer and back button to close drawer
        drawerLayout = findViewById(;
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
        // pass the Open and Close toggle for the drawer layout listener
        // to toggle the button
        // to make the Navigation drawer icon always appear on the action bar
    // override the onOptionsItemSelected()
      // function to implement
    // the item click listener callback
    // to open and close the navigation
      // drawer when the icon is clicked
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        return super.onOptionsItemSelected(item);

Output: Run on Emulator 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!