androidsubmenuandroid-menunavigation-drawer

Android studio NavigationDrawer sub items that change on runtime


I really need your help on editing my navigation drawer. I want to add sub items to it as shown here: example

or here: example2

I have tried to search for similar questions on google but didn't find an answer. To me, its seems that possible solution may be to add a group of items after 3rd item and make this group invisible, but when the user clicks on "parent" it will become visible. However I am not sure how to insert group of items after specific index in menu and that may not be the best solution. So I am asking for experienced developers to help me find the best solution.


Solution

  • For your first approach, you can make a little edit. That SubItem is also an item an instead of putting everything in group you should put in an item. Try this example below:

        <item
            android:id="@+id/nav_category_electronics"
            android:icon="@drawable/ic_email_black_24dp"
            android:title="Electronics">
    
            <menu>
                <item
                    android:id="@+id/nav_subcategory_laptops"
                    android:icon="@drawable/laptop"
                    android:title="Laptops" />
    
                <item
                    android:id="@+id/nav_subcategory_tablet"
                    android:icon="@drawable/tablet3"
                    android:title="Tablets" />
    
                <item
                    android:id="@+id/nav_category_phones"
                    android:icon="@drawable/smartphone"
                    android:title="Mobile Phones" />
    
                <item
                    android:id="@+id/nav_subcategory_gadgets"
                    android:icon="@drawable/ic_developer_mode_black_18dp"
                    android:title="Gadgets" />
            </menu>
        </item>