Android: How to set stroke color for vector drawable programmatically

I am getting into a trouble with VectorDrawable in Android. I have a vector drawable file (.xml) and I want to draw it on bitmap. I managed to load this file and draw it on bitmap. I can change its fill color but the problem is that I cannot change its stroke and color.

Any helps would be appreciated!!!

Thank you!

Here is the drawable file:



Here is the way I load and fill the shape with blue color:

Drawable drawable = getResources().getDrawable(R.drawable.graph_rounded_rectangle);
        drawable.setBounds(0, 0, width, height);
        drawable.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY));
        drawable.draw(canvas);

Solved

As said by Chris Banes in his blog, you can tint your drawable using support library with the following code:

Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_asset);

// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

Try to add group in your xml like this:


    
        
    

Reference here.


You can only access these vector properties in Java API

Vector : name
Vector : width
Vector : height
Vector : viewportWidth
Vector : viewportHeight
Vector : tint
Vector : tintMode
Vector : autoMirrored
Vector : alpha

for setting stoke of vector (color, size etc) by using java code is not reachable. You have to deal it in drawable.


Comments