user6309010
user6309010

Reputation:

How to customize navigation drawer menu

I have to customize my navigation drawer, this is a part of the code in res/menu/activity_main_drawer.xml

<item
    android:title="Discover">
    <menu>
        <item
            android:id="@+id/nav_qrcode"
            android:icon="@drawable/read_qr"
            android:title="QRCode" />

        <item
            android:id="@+id/favorite"
            android:icon="@drawable/favorite"
            android:title="favorite" />
    </menu>
</item>

this is the layout in res/layout/activity_home.xml

<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"
    android:background="#B59154"
    app:itemTextColor="#ffffff"
    app:menu="@menu/activity_main_drawer" />

I want assign to the first item a background color, how i can do this ?

"Discover" is a category, and the others item are manù (clickable).

I don't know how to assign to my "categories" a different layout.

Upvotes: 6

Views: 14293

Answers (1)

KDeogharkar
KDeogharkar

Reputation: 10959

you can always have an option to create your custom layout so you can design it in your way for your navigation drawer. for example.

Navigation View

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">



    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <include layout="@layout/lay_drawer_menu" />

        </LinearLayout>
    </ScrollView>
    </android.support.design.widget.NavigationView>

lay_drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt1"
        android:textcolor = "whatever you want"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Activities" />

    <TextView
        android:id="@+id/txt2"
      android:textcolor = "whatever you want"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Add Detail"
        android:textColor="@android:color/black" />


</LinearLayout>

Activity

  @Override
        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.txt1: {
                    drawerLayout.closeDrawers();

                   //your code.

                }
                break;
               case R.id.txt2: {
                    drawerLayout.closeDrawers();

                   //your code.

                }
                break;

            }

        }

hope this will help.

Upvotes: 12

Related Questions