All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Android: Navigation Drawer Submenu: How To Collapsible Navigation Items

Last Updated : Mar 11, 2024

Android: Navigation Drawer Submenu: How To Collapsible Navigation Items

In this article we will show you the solution of android: navigation drawer submenu: how to collapsible navigation items, all of the activities in my app have a similar navigation drawer, which is a DrawerLayout enclosing a NavigationView.

I'm offering a menu resource for the navigation view's app:menu.

I have a few menu options, but I just want one of them to be expandable, such that when I click on it, it opens to reveal two submenus and shrinks once more.

I've added submenus by putting another menu item inside the original one, but I can't get it to collapse or expand.

Moreover, ExpandableListView is not what I want to utilize for this.

Only a few changes to that same menu resource files are required. Just send me on the right route, please.

I've only been able to get ExpandableListView code, blogs, and examples through Google, but I need them to work with NavigationView design widgets from the design support Library.

Step By Step Guide On Android: Navigation Drawer Submenu: How To Collapsible Navigation Items :-

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        NavigationView nv= (NavigationView) findViewById(R.id.nav_view);
        Menu m=nv.getMenu();
        int id = item.getItemId();
        if (id == R.id.nav_posts) {
            boolean b=!m.findItem(R.id.nav_class).isVisible();
            //setting submenus visible state
            m.findItem(R.id.nav_class).setVisible(b);
            m.findItem(R.id.nav_dept).setVisible(b);
            m.findItem(R.id.nav_batch).setVisible(b);
            m.findItem(R.id.nav_campus).setVisible(b);
            return true;
        } else if (id == R.id.nav_walls) {
            boolean b=!m.findItem(R.id.nav_wall_events).isVisible();
            //setting submenus visible state
            m.findItem(R.id.nav_wall_events).setVisible(b);
            m.findItem(R.id.nav_wall_fun).setVisible(b);
            m.findItem(R.id.nav_wall_hadith).setVisible(b);
            m.findItem(R.id.nav_wall_news).setVisible(b);
            m.findItem(R.id.nav_wall_Poetry).setVisible(b);
            return true;
        } else if (id == R.id.nav_com) {
            m.findItem(R.id.nav_share).setVisible(false);
            m.findItem(R.id.nav_send).setVisible(false);
        } else if (id == R.id.nav_share) {
        } else if (id == R.id.nav_send) {
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

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/nav_posts"
            android:icon="@drawable/ic_menu_camera"
            android:title="Posts" />
        <item
            android:id="@+id/nav_class"
            android:title="Class"
            android:visible="false" />
        <item
            android:id="@+id/nav_dept"
            android:title="Department"
            android:visible="false" />
        <item
            android:id="@+id/nav_batch"
            android:title="Batch"
            android:visible="false" />
        <item
            android:id="@+id/nav_campus"
            android:title="Campus"
            android:visible="false" />
        <item
            android:id="@+id/nav_walls"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Walls" />
        <item
            android:id="@+id/nav_wall_news"
            android:title="News"
            android:visible="false" />
        <item
            android:id="@+id/nav_wall_events"
            android:title="Events"
            android:visible="false" />
        <item
            android:id="@+id/nav_wall_fun"
            android:title="Fun"
            android:visible="false" />
        <item
            android:id="@+id/nav_wall_hadith"
            android:title="Hadith"
            android:visible="false" />
        <item
            android:id="@+id/nav_wall_Poetry"
            android:title="Poetry"
            android:visible="false" />
    </group>
    <item
        android:id="@+id/nav_com"
        android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>
</menu>
  1. Create a new Android Studio project.
  2. Introducing a dependency into the project, The Design Patterns Navigation drawer will be used in this discussion. As a result, also include mentioned Material design dependency in the app-level Gradle file.
  3. Making a menu in the menu file, Make a menu folder in the res folder.
  4. Invoke the javascript command in the activity main.xml file to decided up the basic things necessary for the Route planning Drawer while continuing to work with the activity main.xml file.
  5. Include the Open Close strings in string.xml, then run the code below in the app/res/values/strings.xml file.
  6. Working with the MainActivity File, run the code in the MainActivity file to display the menu button on the activity bar and implement the navigation drawer's open/close functionality.

Conclusion :-

The ability to add Navigation Drawer Activity directly from the IDE is supported by Android Studio.

Such an activity creates menu items for the navigation drawer automatically using an xml file resource that is housed in the res/menu directory.

This XML file has the default name of activity main drawer.xml and statically contains menu items.

I hope this article on android: navigation drawer submenu: how to collapsible navigation items helps you and the steps and mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪