Thang Pham
Thang Pham

Reputation: 1026

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:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="312dp"
    android:height="312dp"
    android:viewportWidth="312.7"
    android:viewportHeight="312.699">
<path
    android:pathData="M306.35,266.34c0,22.09 -17.91,40.01 -40,40.01L46.35,306.35c-22.09,0 -40,-17.92 -40,-40.01v-219.99c0,-22.11 17.92,-40 40,-40h220c22.09,0 40,17.9 40,40L306.35,266.34z"
    android:strokeWidth="5"
    android:fillColor="@color/transparent"
    android:strokeColor="#231F20"/></vector>

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);

Upvotes: 14

Views: 23670

Answers (4)

Style-7
Style-7

Reputation: 1209

Use setTint( newColor ) method.

VectorDrawable vd = (VectorDrawable)context.getDrawable( R.drawable.ic_hint );
vd.setTint( 0xffff0000 ); //for red color

Upvotes: 1

Fakhar Iqbal
Fakhar Iqbal

Reputation: 4069

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.

Upvotes: 1

Context
Context

Reputation: 1883

Try to add group in your xml like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="312dp"
    android:height="312dp"
    android:viewportWidth="312.7"
    android:viewportHeight="312.699">
    <group android:scaleX="1.0" android:scaleY="1.0">
        <path
            android:pathData="M306.35,266.34c0,22.09 -17.91,40.01 -40,40.01L46.35,306.35c-22.09,0 -40,-17.92 -40,-40.01v-219.99c0,-22.11 17.92,-40 40,-40h220c22.09,0 40,17.9 40,40L306.35,266.34z"
            android:strokeWidth="5"
            android:fillColor="@color/transparent"
            android:strokeColor="#231F20"/>
    </group>
</vector>

Reference here.

Upvotes: 6

Filipe Bezerra de Sousa
Filipe Bezerra de Sousa

Reputation: 3032

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);

Upvotes: 8

Related Questions