Anton Kizema
Anton Kizema

Reputation: 1092

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
  />

Upvotes: 12

Views: 31823

Answers (5)

Amit Kundu
Amit Kundu

Reputation: 119

Use app:drawableTint="@color/yourColor" in the xml instade android:drawableTint="@color/yourColor". It has the backward compatibility.

Upvotes: 1

Fellipe Tavares
Fellipe Tavares

Reputation: 114

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"/>

Upvotes: 5

Md Selim
Md Selim

Reputation: 21

Try the following:

android:drawableTint="@color/icon_color"

Upvotes: 2

iscariot
iscariot

Reputation: 434

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>

Upvotes: 11

hrskrs
hrskrs

Reputation: 4490

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

Upvotes: 5

Related Questions