androidvectorvector-graphicsandroid-vectordrawable

How to change color of vector drawable path on button click


With the new android support update, vector drawables get backward compatibility. I have a vector image with various paths. I want the color of the paths to change on click of a button or programmatically based on an input value. Is it possible to access the name parameter of the vector path? And then change the color.


Solution

  • 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