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 :-
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
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.
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.
The code used is: PopupMenu popupMenu = new PopupMenu(mContext, mImageView); popupMenu. setOnMenuItemClickListener(MyClass. this); popupMenu.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With