Change drawable color programmatically


I'm trying to change the color on a white marker image by code. I have read that the code below should change the color, but my marker remains white.

Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )

Did I miss something? Is there any other way to change colors on my drawables located in my res folder?

asked on Stack Overflow Jul 7, 2012 by Johan • edited Jun 21, 2015 by StackExchange What The Heck

17 Answers


Try this:

Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable); 
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);    

Using DrawableCompat is important because it provides backwards compatibility and bug fixes on API 22 devices and earlier.

answered on Stack Overflow Jul 7, 2012 by ρяσѕρєя K • edited Jun 4, 2019 by Alex Lockwood

You can try this for svg vector drawable

    ContextCompat.getColor(context, R.color.another_nice_color)
answered on Stack Overflow Oct 6, 2016 by amorenew • edited Jun 4, 2019 by Alex Lockwood

You may need to call mutate() on the drawable or else all icons are affected.

Drawable icon = ContextCompat.getDrawable(getContext(), R.drawable.ic_my_icon).mutate();
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.colorIcon, typedValue, true);
icon.setColorFilter(, PorterDuff.Mode.SRC_ATOP);
answered on Stack Overflow Jan 21, 2016 by shicky • edited Jul 8, 2016 by shicky

Another way to do this on Lollipop, android 5.+ is setting a tint on a bitmap drawable like such:

<?xml version="1.0" encoding="utf-8"?>

This will work for you if you have a limited number of colors you want to use on your drawables. Check out my blog post for more information.

answered on Stack Overflow Dec 9, 2014 by MinceMan • edited Dec 14, 2016 by MinceMan

You can try this for ImageView. using setColorFilter().

imageViewIcon.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite));
answered on Stack Overflow Jan 16, 2018 by Jaydip Meghapara • edited Mar 2, 2020 by Jaydip Meghapara

I have wrote a generic function in which you can pass context, icon is id drawable/mipmap image icon and new color which you need for that icon.

This function returns a drawable.

public static Drawable changeDrawableColor(Context context,int icon, int newColor) {
    Drawable mDrawable = ContextCompat.getDrawable(context, icon).mutate(); 
    mDrawable.setColorFilter(new PorterDuffColorFilter(newColor, PorterDuff.Mode.SRC_IN)); 
    return mDrawable;

changeDrawableColor(getContext(),R.mipmap.ic_action_tune, Color.WHITE);
answered on Stack Overflow Mar 20, 2017 by Sachin Tanpure • edited Mar 22, 2017 by Sachin Tanpure

You could try a ColorMatrixColorFilter, since your key color is white:

// Assuming "color" is your target color
float r = / 255f;
float g = / 255f;
float b = / 255f;

ColorMatrix cm = new ColorMatrix(new float[] {
        // Change red channel
        r, 0, 0, 0, 0,
        // Change green channel
        0, g, 0, 0, 0,
        // Change blue channel
        0, 0, b, 0, 0,
        // Keep alpha channel
        0, 0, 0, 1, 0,
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
answered on Stack Overflow Jul 7, 2012 by tiguchi • edited Feb 24, 2015 by JJD

This worked for me. Make sure to put "ff" between 0x and color code. Like this 0xff2196F3

Drawable mDrawable = ContextCompat.getDrawable(MainActivity.this,R.drawable.ic_vector_home);
answered on Stack Overflow Nov 15, 2017 by Bek

Same as the accepted answer but a simpler convenience method:

val myDrawable = ContextCompat.getDrawable(context, R.drawable.my_drawable)
myDrawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN)
setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null)

Note, the code here is Kotlin.

answered on Stack Overflow May 18, 2017 by CorayThan

You may want to try Mode.LIGHTEN or Mode.DARKEN. The Android Javadocs are horrible at explaining what the PorterDuff Modes do. You can take a look at them here: PorterDuff | Android

I suggest looking around at Compositing on Mozilla's site here. (They don't have all the modes that android does but they have a lot of them)

answered on Stack Overflow Jul 7, 2012 by sebsebmc • edited Mar 5, 2014 by groovecoder

Use this: For java

view.getBackground().setColorFilter(Color.parseColor("#343434"), PorterDuff.Mode.SRC_OVER)

for Kotlin


you can use PorterDuff.Mode.SRC_ATOP, if your background has rounded corners etc.

answered on Stack Overflow Jul 12, 2019 by Ravi.Dudi


"your image name".setColorFilter("your context".getResources().getColor("color name"));


answered on Stack Overflow May 3, 2019 by Om Prakash Sharma • edited Jun 13, 2019 by ArteFact

For those who use Kotlin a simple extension function:

fun Drawable.tint(context: Context,  @ColorRes color: Int) {
    DrawableCompat.setTint(this, context.resources.getColor(color, context.theme))

and then simply do

background.tint(context, R.color.colorPrimary)
answered on Stack Overflow Jul 8, 2020 by Chapz

This is what i did:

public static Drawable changeDrawableColor(int drawableRes, int colorRes, Context context) {
    //Convert drawable res to bitmap
    final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableRes);
    final Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0,
            bitmap.getWidth() - 1, bitmap.getHeight() - 1);
    final Paint p = new Paint();
    final Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, p);

    //Create new drawable based on bitmap
    final Drawable drawable = new BitmapDrawable(context.getResources(), resultBitmap);
            PorterDuffColorFilter(context.getResources().getColor(colorRes), PorterDuff.Mode.MULTIPLY));
    return drawable;
answered on Stack Overflow May 16, 2016 by Edwin

Simply use


in Your XML file. Replace primary_color to custom color

answered on Stack Overflow Feb 14, 2018 by Arvina Kori • edited Oct 1, 2018 by Gowthaman M

Create Method like this :

private void changeIconColor(Context context ,int drawable){
    Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, drawable);
    assert unwrappedDrawable != null;
    Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
    DrawableCompat.setTint(wrappedDrawable, getResources().getColor(R.color.colorAccent));

and use it like it :

answered on Stack Overflow Sep 2, 2019 by sana ebadi

Easiest Way to do it :

imageView.setColorFilter(Color.rgb(r, g b));


imageView.setColorFilter(Color.argb(a, r, g, b));

a, r, g, b : Color argb values.

answered on Stack Overflow Nov 27, 2019 by Anubhav

User contributions licensed under CC BY-SA 3.0