How to change color of vector drawable path on button click
Use this to change a path color in your vector drawable
VectorChildFinder vector = new VectorChildFinder(this, R.drawable.my_vector, imageView);
VectorDrawableCompat.VFullPath path1 = vector.findPathByName("path1");
path1.setFillColor(Color.RED);
Library is here: https://github.com/devsideal/VectorChildFinder
The color of the whole vector can be changed using setTint.
You have to set up your ImageView in your layout file as this:
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/my_nice_color"
android:src="@drawable/ic_my_drawable"
android:scaleType="fitCenter" />
Then to change the color of your image:
DrawableCompat.setTint(myImageView.getDrawable(), ContextCompat.getColor(context, R.color.another_nice_color));
Note: myImageView.getDrawable()
gives nullpointerexception if the vector drawable is set to the imageView as background.
There are several ways of doing the same stuff, but this works for both Vector Drawables as well as SVG (Local/Network).
imageView.setColorFilter(ContextCompat.getColor(context,
R.color.headPink), android.graphics.PorterDuff.Mode.SRC_IN);
(change R.color.headPink with color of your choice)