Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of xml drawable icon

Is there any way of setting a color to an icon drawable? So that the icon's color would be overwritten by my custom color ?

 <item android:drawable="@drawable/icon1"
  //set color to my icon here
    android:state_pressed="true" />
<item android:drawable="@drawable/icon2"
  //set color to my icon here
  />
like image 608
Anton Kizema Avatar asked May 12 '15 10:05

Anton Kizema


4 Answers

Try the following:

android:drawableTint="@color/icon_color"
like image 113
Md Selim Avatar answered Oct 19 '22 07:10

Md Selim


Use the tint attribute on your image element (or whatever you use to show an image), for example:

<ImageView  android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_drawable_resource_name"
            android:tint="@color/color_name"/>
like image 37
Fellipe Tavares Avatar answered Oct 19 '22 05:10

Fellipe Tavares


You can use this snippet

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <bitmap android:src="@drawable/signup_checkbox_empty" android:tint="@color/turquoise" />
    </item>
    <item android:state_checked="true">
        <bitmap android:src="@drawable/signup_checkbox_full" android:tint="@color/turquoise" />
    </item>
    <item>
        <bitmap android:src="@drawable/signup_checkbox_empty" android:tint="@color/turquoise" />
    </item>
</selector>
like image 11
iscariot Avatar answered Oct 19 '22 05:10

iscariot


In android L (5.0) there is a TINT feature which allows to change the color of a drawable icon. You can check an example here.

For earlier APIs you have to use multiple drawables with selector strategy

like image 5
hrskrs Avatar answered Oct 19 '22 07:10

hrskrs