3

I have created a vector drawable :

ic_item_overflow.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#8b8b8b"
    android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>

I want to use this vector drawable in 2 colors: dark grey color code #8b8b8b and white color : #FFFFFF

I've used the dark grey color code in the vector drawable file so the drawable will be shown in this color by default. I've changed the color of this vector drawable like below:

    Drawable drawable = ContextCompat.getDrawable
(mContext, R.drawable.ic_item_overflow);
DrawableCompat.setTint(drawable, ContextCompat.getColor(mContext, R.color.white));

This is working fine in case of all lollipop and post lollipop devices but not working fine on prelollipop devices. What can I do to get this working in all version of android.

Please help if anyone know how to do this. Thanks a lot in advanced.

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78

2 Answers2

14

Try this code :

imageView.setColorFilter(ContextCompat.getColor(context, R.color.black), PorterDuff.Mode.SRC_IN);
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0
public static Drawable updateDrawableVector(int resId, Context context, int color) {
        Drawable drawable = context.getResources().getDrawable(resId);
        drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
        return drawable;
    }

if you want it to work for even API < 21, this can help

Sep
  • 147
  • 8