Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHow to Check Navigation Drawer Menu Items in Android?

How to Check Navigation Drawer Menu Items in Android?

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: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 
    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"?>
  
            <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:

RELATED ARTICLES

Most Popular

Recent Comments