Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give color to menu items for Navigation drawer?

I was creating Navigation Drawer and i saw playtore have colored menu icons i want to know how can i do this. I tried to apply colors by colorFilter on menu icons but app force closes

My app

what i wanted

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>
like image 780
architjn Avatar asked Jan 10 '16 13:01

architjn


1 Answers

  1. To brush all icons into particular color, you need to add app:itemIconTint into your NavigationView:

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

    enter image description here

  2. To brush your icons in only 2 colors:

    Create a 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 a app:itemIconTint in your NavigationView:

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

    Last step - to add android:checked="true" to the MenuItems in your drawer's menu xml you want to brush different:

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

    enter image description here

  3. To brush all icons in different colors, like Google has in Google Play:

    Disable tinting of your icons:

      navigationView.setItemIconTintList(null);
    

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

    enter image description here

    Instead of uploading new colorful icons, you can paint existing ones, but it's very hacky:

        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);
    

    Watch out! In res/drawables-v21 in template project, Google uses VectorDrawables instead of old BitmapDrawables, so this code won't work there.

I hope, it helps.

like image 155
Konstantin Loginov Avatar answered Oct 01 '22 20:10

Konstantin Loginov