How to Grouping Navigation Drawer Menu Items in Android?
Last Updated :
21 Apr, 2025
Navigation Drawer or Slider Drawer is a UI pattern, used in Mobile Applications and Websites. It is used to provide easy access to different features or parts of an app or website.
Functioning of Navigation Drawer
It is usually accessed by tapping on the hamburger menu icon (3 horizontal lines), present in the top-left or top-right corner of the screen, or swiping from the left edge of the screen.
Components of Navigation Drawer
- Drawer Header Layout: Contains additional information, such as User’s profile picture or App Name/ Website Name, etc.
- Menu: Contains a list of options or menu items that the user can navigate to. Users can select and menu item by clicking on it, then it will load a new screen based on the backend of the app or website.
- Improves User Experience
- Ensures the correct functioning of the app
- Maintaining a cohesive user interface
- Makes a user-friendly experience
- Reduces vagueness
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Adding Material Design dependency in build.gradle.kts (Module g:app)
dependencies {
...
implementation ("com.google.android.material:material:1.12.0")
}
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- Main Content -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#2F8C46"
app:title="GeeksForGeeks"
app:titleTextColor="@android:color/white" />
</com.google.android.material.appbar.AppBarLayout>
<!-- Main Fragment or Content Area -->
<FrameLayout
android:id="@+id/layFL"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- Navigation Drawer -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@color/black"
android:fitsSystemWindows="true"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/nav_header" />
</androidx.drawerlayout.widget.DrawerLayout>
Navigate to the app > res > layout > nav_header.xml and add the below code to that file. Below is the code for the nav_header.xml file.
nav_header.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#2F8C46"
android:gravity="bottom"
android:padding="15dp"
android:layout_height="180dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="32dp"
android:src="@drawable/gfg_logo"
app:tint="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GeeksForGeeks"
android:layout_marginStart="12dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
</LinearLayout>
Navigate to the app > res > menu > nav_menu.xml and add the below code to that file. Make sure to add a relatable menu icon in the drawable file. Below is the code for the nav_menu.xml file.
nav_menu.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="General">
<menu>
<item
android:id="@+id/row_home"
android:title="Home"
android:icon="@android:drawable/ic_menu_mylocation">
</item>
<item
android:title="Favourites"
android:id="@+id/row_fav"
android:icon="@android:drawable/ic_menu_mylocation">
</item>
<item android:title="Profile"
android:id="@+id/row_profile"
android:icon="@android:drawable/ic_menu_mylocation"/>
</menu>
</item>
<item android:title="Communication">
<menu>
<item android:title="Settings"
android:id="@+id/row_settings"
android:icon="@android:drawable/ic_menu_mylocation"/>
<item
android:title="Share"
android:id="@+id/row_share"
android:icon="@android:drawable/ic_menu_mylocation">
</item>
<item android:title="Bug Fix"
android:id="@+id/row_bugfix"
android:icon="@android:drawable/ic_menu_mylocation"/>
<item
android:title="Suggestions"
android:id="@+id/row_suggestions"
android:icon="@android:drawable/ic_menu_mylocation">
</item>
</menu>
</item>
</menu>
Step 6: Working with the MainActivity file
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
MainActivity File:
MainActivity.java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.navigationView);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.open_drawer,
R.string.close_drawer
);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
navigationView.setCheckedItem(R.id.row_home);
}
setNavigationActions();
}
private void setNavigationActions() {
navigationView.setNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.row_home:
showToast("Home");
break;
case R.id.row_fav:
showToast("Favourites");
break;
case R.id.row_profile:
showToast("Profile");
break;
case R.id.row_settings:
showToast("Settings");
break;
case R.id.row_share:
showToast("Share");
break;
case R.id.row_bugfix:
showToast("Bug Fix");
break;
case R.id.row_suggestions:
showToast("Suggestions");
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
});
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
@Override
@Deprecated
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
MainActivity.kt
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var navigationView: NavigationView
private lateinit var toolbar: Toolbar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
drawerLayout = findViewById(R.id.drawerLayout)
navigationView = findViewById(R.id.navigationView)
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.open_drawer,
R.string.close_drawer
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
if (savedInstanceState == null) {
navigationView.setCheckedItem(R.id.row_home)
}
setNavigationActions()
}
private fun setNavigationActions() {
navigationView.setNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.row_home -> showToast("Home")
R.id.row_fav -> showToast("Favourites")
R.id.row_profile -> showToast("Profile")
R.id.row_settings -> showToast("Settings")
R.id.row_share -> showToast("Share")
R.id.row_bugfix -> showToast("Bug Fix")
R.id.row_suggestions -> showToast("Suggestions")
}
drawerLayout.closeDrawer(GravityCompat.START)
true
}
}
private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
}
Step 7: Working with strings.xml
Navigate app > res > values > strings.xml and add the following values
<resources>
...
<string name="open_drawer">Open navigation drawer</string>
<string name="close_drawer">Close navigation drawer</string>
</resources>
Output:
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read