How to set up the navigation box menu

I need to configure my navigation box, this is 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 to assign a background color to the first element, how can I do this?

"Discovery" is a category, and the rest is manù (clickable).

I don’t know how to assign a different layout to my “categories”.

+4
source share
1 answer

You always have the opportunity to create your own custom layout, so that you can design it for your navigation box. eg.

Type of navigation

<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 helps.

+6
source

All Articles