Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of text and icon of Sub-Menu attached to Navigation view?

I am trying to figure out how I can change color of sub-menu items which is actually attached to navigation view. Following codes are actually from default template of Navigation Drawer which is available in android studio.

activity_main_drawer.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_camara"
            android:icon="@android:drawable/ic_menu_camera"
            android:title="Import"
            />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@android:drawable/ic_menu_gallery"
            android:title="Gallery"/>
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@android:drawable/ic_menu_slideshow"
            android:title="Slideshow"/>
        <item
            android:id="@+id/nav_manage"
            android:icon="@android:drawable/ic_menu_manage"
            android:title="Tools"/>
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@android:drawable/ic_menu_share"
                android:title="Share"/>
            <item
                android:id="@+id/nav_send"
                android:icon="@android:drawable/ic_menu_send"
                android:title="Send"/>
        </menu>
    </item>

</menu>

activity_main.xml

    <include
        layout="@layout/app_bar_main"
        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"
        android:background="#512DA8"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        app:itemTextColor="@drawable/nav_menu_item_color"
        app:itemIconTint="@drawable/nav_menu_item_color"/>

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

and I have a drawable file for click and normal color which is actualy used above. nav_menu_item_color.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<selector>
    <item android:color="@color/navTextHover" android:state_checked="true" />
    <item android:color="@color/navTextNormal" />


</selector>

So, you can see result image as I attached below :-Color of Sub-Menu Items didn't change

Colors not working for sub menu, so, what I want to do is .. I want to change color of menu item "communicate" and sub-menu item "send" and "share" as it is working for root menu items

like image 658
Vivek Kumar Avatar asked Oct 07 '15 14:10

Vivek Kumar


People also ask

How do I change the color of the menu text?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 4 – Right-click on res, select New -> Android Resource Directory – menu.

How do I change the menu color?

Open the colors. xml file by navigating to the app -> res -> values -> colors. xml. Create a color tag inside the resources tag with a name and set a color with its hex code.

How do I change the text color of popup menu in android programmatically?

The code used is: PopupMenu popupMenu = new PopupMenu(mContext, mImageView); popupMenu. setOnMenuItemClickListener(MyClass. this); popupMenu.


1 Answers

Your submenu needs to be wrapped in a menu & group tag like below. This will allow you to select one of any of the menu items at a time. You can select them by setting the item as checked in your NavigationView.OnNavigationItemSelectedListener.

<?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/nav1"
            android:checked="true"
            android:icon="@drawable/myd1"
            android:title="Nav 1"
            />
    </group>
    <item android:title="@string/nav_item_subheading_app">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav1"
                    android:icon="@drawable/myd1"
                    android:title="Nav 1"
                    />
            </group>
        </menu>
    </item>
</menu>
like image 191
fractalwrench Avatar answered Sep 29 '22 17:09

fractalwrench