Search code examples
androidnavigation-drawer

DropDown menu in Nav Bar android


I am busy trying to create a dropdown menu from the default navigation bar option that i used in android studio. I have read many places that you need to use expandable List view but i am not sure how to implement it because i am use to coding. So here is my menudrawer.xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">




        <item
            android:id="@+id/nav_add"
            android:icon="@drawable/ic_playlist_add_black_24dp"
            android:title="Add Products" />

        <item
            android:id="@+id/nav_up"
            android:icon="@drawable/ic_create_black_24dp"
            android:title="Update or delete Products" />

        <item
            android:id="@+id/nav_cus"
            android:icon="@drawable/ic_local_library_black_24dp"
            android:title="Customers" />

        <item android:id="@+id/calc"
            android:icon="@drawable/baseline_tablet_black_18dp"
            android:title="Calculator"/>

--This part under the title Calculator i want a drop down which has different calculator options


    </group>



    <item android:title="Other Functions">
        <menu>

            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Logout" />
        </menu>
    </item>



</menu>

And here is my main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/background"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_emily"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_emily"
        app:menu="@menu/activity_emily_drawer" />

</android.support.v4.widget.DrawerLayout>

and here is my .java file:

    @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_add) {
        Intent i = new Intent(this,Products.class);
        startActivity(i);
        // Handle the camera action



    } else if (id == R.id.nav_send) {
        Intent jj = new Intent(this,login.class);
        startActivity(jj);

    }
    else if (id == R.id.nav_up) {
        Intent jj = new Intent(this,updelProducts.class);
        startActivity(jj);

    }

    else if (id == R.id.nav_cus) {
        Intent z = new Intent(this,Customer.class);
        startActivity(z);

    }

    else if (id == R.id.calc) {
        Intent jj = new Intent(this, Calc_140_plain.class);
        startActivity(jj);

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Now i would like to add a drop down menu under the calculator tab where i have different calculators that the takes the user to an appropriate page


Solution

  • If I understood you correctly, you want to show different items inside your menu item. You can do this easily by using <menu> inside the <item>.

    <menu 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"
        tools:context=".MainActivity">
    
    <item
        android:id="@+id/firstOne"
        android:orderInCategory="1"
        android:title="First One"
        app:showAsAction="ifRoom">
        <menu>
            <item
                android:id="@+id/menuSortNewest"
                android:title="1-1" />
            <item
                android:id="@+id/menuSortRating"
                android:title="1-2" />
        </menu>
    </item>
    
    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="2"
        android:title="Second One"
        app:showAsAction="ifRoom"/>
    </menu>
    

    For example, the code above will show two different items called First One and Second One. When you click the First One, you will see two different options called 1-1 and 1-2. I hope, this helps.

    Have a beautiful day!