Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of a checked menu item in a navigation drawer

I am using the new Android Design Support library to implement a navigation drawer in my application.

I can't figure out how to change the color of a selected item!

Here is the xml of the menu :

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single">     <item         android:id="@+id/navigation_item_1"         android:icon="@drawable/ic_1"         android:title="@string/navigation_item_1"/>      <item         android:id="@+id/navigation_item_2"         android:icon="@drawable/ic_2"         android:title="@string/navigation_item_2"/> </group> 

And here is the navigationview xml which is placed inside a android.support.v4.widget.DrawerLayout :

<android.support.design.widget.NavigationView     android:id="@+id/activity_main_navigationview"     android:layout_width="wrap_content"     android:layout_height="match_parent"     android:layout_gravity="start"     app:headerLayout="@layout/drawer_header"     app:itemIconTint="@color/black"     app:itemTextColor="@color/primary_text"     app:menu="@menu/menu_drawer">      <TextView         android:id="@+id/main_activity_version"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="bottom"         android:layout_marginBottom="@dimen/activity_vertical_margin"         android:layout_marginLeft="@dimen/activity_horizontal_margin"         android:textColor="@color/primary_text" />  </android.support.design.widget.NavigationView> 

Thank you for your help !

[EDIT] I have already looked at solutions such as this one : Change background color of android menu.

It seems to be quite a hack and I thought that with the new Design Support Library, something cleaner would have been introduced?

like image 895
Greg Avatar asked Jun 17 '15 08:06

Greg


People also ask

How do I change the navigation drawer item color?

Open res > values > colors. xml and add the following line for the new icon and text color for when an item is selected: 5. In your styles.

How do I customize my navigation drawer?

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 3 − Add the following code to res/layout/nav_header_main.


1 Answers

Well you can achieve this using Color State Resource. If you notice inside your NavigationView you're using

app:itemIconTint="@color/black" app:itemTextColor="@color/primary_text" 

Here instead of using @color/black or @color/primary_test, use a Color State List Resource. For that, first create a new xml (e.g drawer_item.xml) inside color directory (which should be inside res directory.) If you don't have a directory named color already, create one.

Now inside drawer_item.xml do something like this

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:color="checked state color" android:state_checked="true" />     <item android:color="your default color" /> </selector> 

Final step would be to change your NavigationView

<android.support.design.widget.NavigationView     android:id="@+id/activity_main_navigationview"     android:layout_width="wrap_content"     android:layout_height="match_parent"     android:layout_gravity="start"     app:headerLayout="@layout/drawer_header"     app:itemIconTint="@color/drawer_item"  // notice here     app:itemTextColor="@color/drawer_item" // and here     app:itemBackground="@android:color/transparent"// and here for setting the background color to tranparent     app:menu="@menu/menu_drawer"> 

Like this you can use separate Color State List Resources for IconTint, ItemTextColor, ItemBackground.

Now when you set an item as checked (either in xml or programmatically), the particular item will have different color than the unchecked ones.

like image 195
Sash_KP Avatar answered Oct 21 '22 21:10

Sash_KP