There was not a difficult, but incomprehensible problem for me. Perhaps this question has already been raised, which would be obvious, but I did not find the answer.

There is a vector image (SVG), which we convert to xml and later display it in ImageView . The question is how I can set the <path>, fillColor <path>, the fillColor attribute from the java-code.

    1 answer 1

    Maybe something like this:

     DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(context, R.color.yourColor)); 

    or such:

     public class TintIcon { private static int[][] states = new int[][] { new int[] { android.R.attr.state_pressed}, new int[] { android.R.attr.state_focused}, new int[] { android.R.attr.state_selected}, new int[] { -android.R.attr.state_selected} }; private static int[] colors = new int[] { Color.parseColor("#4EA62E"), // primary color Color.parseColor("#4EA62E"), // primary color Color.parseColor("#4EA62E"), // primary color Color.parseColor("#868686") // grey icon }; private static int[] colorsSwitch = new int[] { Color.parseColor("#D7B00F"), // yellow color Color.parseColor("#D7B00F"), // yellow color Color.parseColor("#D7B00F"), // yellow color Color.parseColor("#868686") // grey icon//// }; private static ColorStateList myColorStateList = new ColorStateList(states,colors); private static ColorStateList myColorStateListSwitch = new ColorStateList(states,colorsSwitch); public static Drawable tintIconToGreenColor(Context context, Drawable icon) { if (icon != null) { icon = DrawableCompat.wrap(icon).mutate(); DrawableCompat.setTintList(icon, myColorStateList ); DrawableCompat.setTintMode(icon, PorterDuff.Mode.SRC_IN); } return icon; } public static Drawable tintIconYellowColor(Context context, Drawable icon) { if (icon != null) { icon = DrawableCompat.wrap(icon).mutate(); DrawableCompat.setTintList(icon, myColorStateListSwitch ); DrawableCompat.setTintMode(icon, PorterDuff.Mode.SRC_IN); } return icon; } } 

    These two methods tintIconToGreenColor and tintIconYellowColor color the icon in green and yellow. I use it to change the color of the picture on the gray icon.

    I paint this icon in .png in two different colors:

    enter image description here enter image description here enter image description here

    • It floods completely, but how then, in this case, refer directly to the "<path>"? - Sergey Neykovich
    • Then xs, look here, it can help with something. Do you fundamentally use svg? - Kirill Stoianov
    • In principle, no, it does not matter. The task is to paint the figure by tapu, the color I need, provided that the color is not known in advance and the number of the figures in the picture, too. - Sergey Neykovich
    • @ SergeyNeikovich I updated the answer - Kirill Stoianov