How to Change Color of Vector Drawable Path on Button Click

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

changing vector drawable color

Use this class

private class VectorDrawableUtils {

Drawable getDrawable(Context context, int drawableResId) {
return VectorDrawableCompat.create(context.getResources(), drawableResId, context.getTheme());
}

Drawable getDrawable(Context context, int drawableResId, int colorFilter) {
Drawable drawable = getDrawable(context, drawableResId);
drawable.setColorFilter(ContextCompat.getColor(context, colorFilter), PorterDuff.Mode.SRC_IN);
return drawable;
}

}

And call it like this

 new VectorDrawableUtils().getDrawable(mContext,R.drawable.x,R.color.black); 

Change fill color on vector asset in Android Studio

Don't edit the vector assets directly. If you're using a vector drawable in an ImageButton, just choose your color in android:tint.

<ImageButton
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/button"
android:src="@drawable/ic_more_vert_24dp"
android:tint="@color/primary" />

change color of drawable in a button Android

If using AppCompatButton is not your option you can always change backgroundTint of the button to your desired color:

 <Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="8dp"
android:background="@drawable/ic_baseline_change_circle_24"
android:backgroundTint="@color/blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"></Button>

how to change the color of vector asset in FloatingActionButton?

try this with app:srcCompat and app:tint:

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:focusable="true"
app:srcCompat="@drawable/ic_camera_24"
app:tint="@color/green"
app:fabSize="mini"
app:rippleColor="@color/green" />

src: https://material.io/components/buttons-floating-action-button/android#mini-fabs



Related Topics



Leave a reply



Submit