In drawable there is isonka in png format. Black with a transparent background. How to change its color programmatically, so that you don’t have to add the same icon to the drawable only in gray, white, etc.

    1 answer 1

    So:

     public static Drawable tintIcon(Context context, @NotNull Drawable icon, int color) { icon = DrawableCompat.wrap(icon).mutate(); DrawableCompat.setTintList(icon, ContextCompat.getColorStateList(context, color)); DrawableCompat.setTintMode(icon, PorterDuff.Mode.SRC_IN); return icon; } 

    color is either a single color or a ColorStateList .

    For API> = 21, it is sufficient to specify the corresponding tint attributes in the markup.

    • It is probably worth noting that DrawableCompat is a class from the support.v4 support library, and setTint() and similar methods for tinting were added only from version 22.1.0 - pavlofff
    • @pavlofff comments are correct, although few people write without support, and 22.1 came out more than a year ago, the studio throws mud at us, saying that the pier has not been updated until now ... - Yura Ivanov