How to change vector drawable stroke color or fill color programmatically

Viewed 128

I have some vector drawables in my app and I want to customize it's stroke color and fill color programmatically based on the user preferences

My drawable is like the following

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="14dp"
    android:height="12dp"
    android:viewportWidth="14"
    android:viewportHeight="12">
  <path
      android:pathData="M5.6667,10.6667L1,6M1,6L5.6667,1.3334M1,6L13,6"
      android:strokeLineJoin="round"
      android:strokeWidth="2"
      android:fillColor="#00000000"
      android:strokeColor="#6366F1"
      android:strokeLineCap="round"/>
</vector>

I already tried change the tint color using the following ways and doesn't work

1-

binding.statusIv.setColorFilter(Color.parseColor(color))

2-

DrawableCompat.setTint(
    DrawableCompat.wrap(binding.statusIv.drawable),
    Color.parseColor(color)
)

3-

    val drawable = binding.statusIv.drawable.mutate()
    val wrappedDrawable = DrawableCompat.wrap(drawable)

    DrawableCompat.setTint(wrappedDrawable, Color.parseColor(color))
    DrawableCompat.setTintMode(wrappedDrawable, PorterDuff.Mode.SRC_IN)
0 Answers
Related