How Do I Code a Navigation Drawer for an Android App?

Android, Android Apps

Are you looking to add a navigation drawer to your Android app? Navigation drawers are a popular design pattern that allow users to easily navigate between different sections of an app. In this tutorial, we’ll go through the steps to code a navigation drawer for your Android app.

Step 1: Create a new project

The first step is to create a new Android project in Android Studio. Give your project a name and select the minimum SDK version you want to support.

Step 2: Add the Navigation Drawer Layout

Navigate to the res folder and create a new layout file called “activity_main.xml”. In this file, we will add the navigation drawer layout.

First, we need to add the following code inside our main layout:



This code sets up our DrawerLayout with an id of “drawer_layout”.

Next, we need to add our navigation view inside the DrawerLayout. The navigation view is where we will add our menu items for the user to navigate through.



This code adds the NavigationView with an id of “nav_view” and sets its width to wrap content. The layout_gravity attribute sets it on the left side of our screen.

Step 3: Add Menu Items

Now that we have our NavigationView set up, we can add menu items for the user to navigate through. Navigate to the res folder and create a new directory called “menu”. Inside that directory, create a new file called “nav_menu.



This code adds two menu items for the user to navigate between – Home and Profile. The checkableBehavior attribute ensures that only one item can be selected at a time.

Step 4: Set Up Navigation Drawer in Java Code

Next, we need to set up our navigation drawer in our Java code. Navigate to your file and add the following code:

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;

protected void onCreate(Bundle savedInstanceState) {

mDrawerLayout = findViewById(;

NavigationView navigationView = findViewById(R.nav_view);
new NavigationView.OnNavigationItemSelectedListener() {
public boolean onNavigationItemSelected(MenuItem menuItem) {


switch (menuItem.getItemId()) {
case R.nav_home:
// Handle Home Item Clicked
return true;
case R.nav_profile:
// Handle Profile Item Clicked
return true;
return true;


This code sets up our navigation drawer with an instance of DrawerLayout and NavigationView. We also set a listener on our NavigationView to handle clicks on our menu items.

Now, when the user clicks on a menu item, the app will close the navigation drawer and handle the selected menu item accordingly.

Step 5: Test Your Navigation Drawer

Run your app and test your navigation drawer. You should see a hamburger icon in your toolbar that, when clicked, will open up the navigation drawer. Clicking on a menu item should close the navigation drawer and take you to the appropriate screen.


Congratulations! You have successfully coded a navigation drawer for your Android app.

Navigation drawers are an essential design pattern for any app that requires easy access to different sections of content. With this tutorial, you should be able to add a navigation drawer to your own app with ease.