0

There is an image consisting of two colors: black and red. I want to dynamically change the red color to any other color based on the selection of the user. The black color is constant.

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
  <path
      android:pathData="M4,15h15v6h-15z"
      android:fillColor="@color/fluent_red"/>
  <path
      android:pathData="M17.75,14.5005C18.9926,14.5005 20,15.5079 20,16.7505V19.7504C20,20.993 18.9926,22.0004 17.75,22.0004H5.25C4.0074,22.0004 3,20.993 3,19.7504V16.7505C3,15.5079 4.0074,14.5005 5.25,14.5005H17.75ZM17.75,16.0005H5.25C4.8358,16.0005 4.5,16.3363 4.5,16.7505V19.7504C4.5,20.1646 4.8358,20.5004 5.25,20.5004H17.75C18.1642,20.5004 18.5,20.1646 18.5,19.7504V16.7505C18.5,16.3363 18.1642,16.0005 17.75,16.0005ZM7.0525,11.9694L10.8061,2.4743C11.0415,1.8788 11.8486,1.8441 12.1508,2.3698L12.2013,2.4749L15.9477,11.9754C16.0997,12.3607 15.9105,12.7962 15.5251,12.9482C15.1719,13.0875 14.7765,12.9401 14.596,12.6178L14.5523,12.5256L13.556,10H9.443L8.4475,12.5209C8.3079,12.874 7.9301,13.0621 7.5714,12.9737L7.4743,12.9426C7.1212,12.803 6.9331,12.4253 7.0215,12.0666L7.0525,11.9694L10.8061,2.4743L7.0525,11.9694ZM11.5027,4.7923L10.036,8.5H12.964L11.5027,4.7923Z"
      android:fillColor="@color/black"/>
</vector>

Icon containing red and black colors:

icon containing red and black colors

I tried setColorFilter(color) and setTint(color). These changes the color of whole icon.

Result:

Result

Desired:

Desired

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
kaku
  • 1
  • 4
  • Maybe [this](http://emmav.me/posts/2015-nov-29-dynamic-svg-colours/) and [this](https://stackoverflow.com/q/33126904/2289835) answer help you. [This library](https://github.com/devendroid/VectorChildFinder) worked for me. – Rumit Patel Feb 17 '23 at 12:31

0 Answers0