How to specify the color of menu items for the navigation box?

I created a navigation box and I saw that the playtore have color menu icons. I want to know how I can do this. I tried applying colorFilter colors on the menu icons, but the power of the application is closing

My application

what I need

This is my code.

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/grp1" android:checkableBehavior="single"> <item android:id="@+id/navigation_songs" android:checked="true" android:icon="@drawable/ic_audiotrack_white_24dp" android:title="@string/songs" /> <item android:id="@+id/navigation_albums" android:icon="@drawable/ic_album_white_24dp" android:title="@string/albums" /> <item android:id="@+id/navigation_artist" android:icon="@drawable/ic_person_white_24dp" android:title="@string/artists" /> <item android:id="@+id/navigation_playlist" android:icon="@drawable/ic_playlist_play_white_24dp" android:title="@string/playlist" /> </group> <group android:id="@+id/grp2" android:checkableBehavior="none"> <item android:id="@+id/navigation_about" android:icon="@drawable/ic_info_white_24dp" android:title="@string/about" /> <item android:id="@+id/navigation_settings" android:icon="@drawable/ic_settings_white_24dp" android:title="@string/settings" /> </group> </menu> 
+6
source share
1 answer
  • To clear all the icons to a specific color, you need to add app:itemIconTint to your NavigationView :

     <android.support.design.widget.NavigationView ........ app:itemIconTint="<your color>"/> 

    enter image description here

  • To paint your icons in just 2 colors :

    Create selector:

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#0000FF" android:state_checked= "true" /> <item android:color="#FF0000" /> </selector> 

    And apply it as app:itemIconTint in NavigationView :

     <android.support.design.widget.NavigationView ........ app:itemIconTint="@drawable/tint_color_selector"/> 

    The last step is to add android:checked="true" to the MenuItems in your xml menu of the cursor you want to clean:

      <item android:id="@+id/nav_slideshow" android:checked="true" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow" /> 

    enter image description here

  • To clear all icons in different colors, such as Google on Google Play:

    Turn off tinting of your icons:

      navigationView.setItemIconTintList(null); 

    Add the icons to res/drawable and specify them as android:icon in your xml menu. (I can recommend a good service for android icons8.com/web-app/new-icons/android )

    enter image description here

    Instead of loading new bright icons, you can draw existing, but very hacks:

      Drawable oldIcon = navigationView .getMenu() .findItem(R.id.nav_gallery) .getIcon(); if (!(oldIcon instanceof BitmapDrawable)) { return; } Bitmap immutable = ((BitmapDrawable)oldIcon).getBitmap(); Bitmap mutable = immutable.copy(Bitmap.Config.ARGB_8888, true); Canvas c = new Canvas(mutable); Paint p = new Paint(); p.setColor(Color.RED); p.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY)); c.drawBitmap(mutable, 0.f, 0.f, p); BitmapDrawable newIcon = new BitmapDrawable(getResources(), mutable); navigationView .getMenu() .findItem(R.id.nav_gallery) .setIcon(newIcon); 

    Beware! In res/drawables-v21 in the template project, Google uses VectorDrawable instead of the old BitmapDrawables , so this code does not work there.

Hope this helps.

+10
source

All Articles