Android: Tint using DrawableCompat Android: Tint using DrawableCompat android android

Android: Tint using DrawableCompat


In case anyone needs to use DrawableCompat's tinting without affecting other drawables, here's how you do it with mutate():

Drawable drawable = getResources().getDrawable(R.drawable.some_drawable);Drawable wrappedDrawable = DrawableCompat.wrap(drawable);wrappedDrawable = wrappedDrawable.mutate();DrawableCompat.setTint(wrappedDrawable, getResources().getColor(R.color.white));

Which can be simplified to:

Drawable drawable = getResources().getDrawable(R.drawable.some_drawable);drawable = DrawableCompat.wrap(drawable);DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.white));


Previously tinting was not supported by DrawableCompat.Starting from support library 22.1 you can do that, but you need do it in this way:

Drawable normalDrawable = getResources().getDrawable(R.drawable.drawable_to_tint);Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.colorPrimaryLight));


The simplest way to tint cross-platform (if you don't need a ColorStateList) is:

drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);

Don't forget to mutate the Drawable before applying the filter.