Search code examples
androiddrawableandroid-appcompattint

Why DrawableCompat.setTint() method is not woking correct?


I have a question. Why when i use

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

is not working, and when i use

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

is working?

In documentation is like

public static void setTint(@NonNull Drawable drawable, @ColorInt int tint){}

so means i need to provide a resource, not an int.

Thank you.

Edit:

I see if my function is annotated with @ColorInt, (..., @ColorInt int color), is working if i provide a color resource. Is getting more confusing. If is not, resource color is ignored.


Solution

  • You have to resolve the color before using it. R.color.white is just a pointer to the id in R file.

    By invoking getResources().getColor(R.color.white) you resolve the color.