How to Check Navigation Drawer Menu Items in Android?
Last Updated :
24 Apr, 2025
Navigation Drawer or Slider Drawer is a UI component that is used in Mobile Applications and Websites to provide easy access to different features or parts of an app or website.
Functioning of Navigation Drawer
It is generally accessed by tapping on the hamburger menu icon (3 horizontal lines), present in the top-left or top-right corner of the screen.
Parts of the Navigation 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.
Checking Navigation Drawer Items
For Checking Navigation Drawer Items in Android Studio, setCheckedItem() function is used.
Reasons for Checking Menu Items
- Improves User Experience
- Ensures the correct functioning of the app
- Maintaining a cohesive user interface
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. Note that select Java as the programming language.
Step 2: Adding Material Design dependency in build.gradle(Module:app)
implementation 'com.google.android.material:material:1.8.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.
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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layDL"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".MainActivity">
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2F8C46"
app:title="GeeksForGeeks"
app:titleTextColor="@color/white"
android:id="@+id/toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layFL" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Checking Navigation Drawer Item"
android:textColor="#2F8C46"
android:textStyle="bold|italic"
android:textSize="20sp" />
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/vNV"
app:menu="@menu/nav_item2"
app:headerLayout="@layout/nav_header"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
Step 4: Working with the nav_header.xml file
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.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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_marginLeft="32dp"
android:src="@drawable/gfg_logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GeeksForGeeks"
android:layout_marginLeft="12dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
</LinearLayout>
Step 5: Working with the nav_menu2.xml file
Navigate to the app > res > menu > nav_menu2.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_menu2.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single">
<item
android:id="@+id/row_home"
android:title="Home"
android:icon="@drawable/home">
</item>
<item
android:title="Favourites"
android:id="@+id/row_fav"
android:icon="@drawable/fav">
</item>
<item android:title="Profile"
android:id="@+id/row_profile"
android:icon="@drawable/profile"/>
<item android:title="Settings"
android:id="@+id/row_settings"
android:icon="@drawable/settings"/>
<item
android:title="Share"
android:id="@+id/row_share"
android:icon="@drawable/share">
</item>
</group>
</menu>
Step 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
package com.anas.gfgcheckingnavitems;
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 androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
DrawerLayout layDL;
NavigationView vNV;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layDL = findViewById(R.id.layDL);
vNV = findViewById(R.id.vNV);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, layDL, toolbar, R.string.open_drawer, R.string.close_drawer);
layDL.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
vNV.setCheckedItem(R.id.row_home);
}
NavClick();
}
private void NavClick() {
vNV.setNavigationItemSelectedListener(item -> {
Fragment frag = null;
switch (item.getItemId()) {
case R.id.row_home:
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
break;
case R.id.row_profile:
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
break;
case R.id.row_fav:
Toast.makeText(this, "Favourites", Toast.LENGTH_SHORT).show();
break;
case R.id.row_settings:
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
break;
case R.id.row_share:
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
break;
}
layDL.closeDrawer(GravityCompat.START);
return true;
});
}
@Override
public void onBackPressed() {
Fragment currFrag = getSupportFragmentManager().findFragmentById(R.id.layFL);
if (layDL.isDrawerOpen(GravityCompat.START)){
layDL.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}
}
Output:
Similar Reads
How to Grouping Navigation Drawer Menu Items in Android?
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 DrawerIt is usually accessed by tapping on the hamburger menu icon (3 horizontal lines), pres
5 min read
How to Add Icons in Navigation Drawer in Android?
Android navigation drawer is a user interface that shows the app's main navigation menu. It appears when the user touches the drawer icon in the app bar or when the user swipes their fingers or does some kind of action on the screen. It is not visible by default and it needs to open either by slidin
2 min read
How to Change the Text Font of Side Navigation Drawer Items in Android?
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
2 min read
How to Hide NavigationBar in Android?
NavigationBar in Android is a row comprising the back button, home button, and Recent button located at the bottom of the application. Most Android 5.0 Lollipop and above devices have no physical navigation buttons and hence come as buttons present on the screen throughout the screen life. However,
2 min read
How to Create Swipe Navigation in Android?
When talking about Android Apps, the first thing that comes to mind is variety. There are so many varieties of Android apps providing the user with a beautiful dynamic UI. One such feature is to navigate our Android Apps using left and right swipes as opposed to clicking on buttons. Not only does it
6 min read
Navigation Drawer in Android
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, for example changing the user profile, changing the set
6 min read
How to Listen to Orientation Change in Android?
In Android, applications can have orientations of types namely portrait and landscape. By default, every new project when created comes with a portrait orientation. However, this can be changed to landscape or semi. In the case of the semi, both portrait and landscape orientation is supported by the
3 min read
Navigation Drawer in Java Android
A Navigation Drawer is the UI panel that can show the app's main navigation menu. When the user swipes from the left edge of the screen or touches the app icon in the action bar and the drawer appears. It can provide a simple way to navigate between the different sections of the application. This ar
5 min read
How to Create Option Menu in Android using Kotlin?
In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
2 min read
Android Jetpack Compose - Implement Navigation Drawer
Jetpack Compose is a new UI toolkit from Google used to create native Android UI. It speeds up and simplifies UI development using less code, Kotlin APIs, and powerful tools. Prerequisites Familiar with Kotlin and OOP ConceptsBasic understanding of Jetpack ComposeThe navigation drawer is the most us
3 min read