'[Android]How to modify the fillColor of VectDrawable by java Code

I want to modify the fillColor of the VectDrawable by java Code, I cannot find any solution to modify it,

vector xml is like this
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="14dp" android:height="14dp" android:viewportWidth="42" android:viewportHeight="42">
    <path android:fillColor="#fffff" android:pathData=""/>
    <path android:fillColor="#000000" android:pathData=""/>
</vector>

the vect



Solution 1:[1]

I am not an expert in Android Development, so there may be better ways of doing this! But as a quick fix, you could do something like this:

View view = findViewById(R.id.<yourView>);
Drawable drawable = view.getBackground();

Then once you have the drawable you can set the tint, color filter, whatever else it is you may want to do.

drawable.setTint(context.getResources().getColor(R.color.red));

I am not able to test this right now, so I may have made a mistake or two, but Android Studio's error messages should guide you through fixing that. You may also have to call view.setBackground(drawable) to apply the changes. I am not sure if it will be updated automatically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VectorsAreCool123