I\'m facing a problem with the new backward compatibility with VectorDrawables. In the Support Library 23.2 was a new feature for backward compatibility with Android VectorD
After watching What's new in the support library - Google I/O 2016 I noticed one useful method in the AppCompatResources
class. This is AppCompatResources#getColorStateList(Context context, int resId)
. With a help of this method I've implemented selector with vector drawables. Here is my color selector file icon_selector
:
And there is java method that returns tinted drawable:
private Drawable getTintedDrawable(@DrawableRes int drawableId) {
Drawable drawable;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
drawable = getResources().getDrawable(drawableId, getTheme());
} else {
drawable = getResources().getDrawable(drawableId);
}
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(drawable.mutate(), AppCompatResources.getColorStateList(this, R.color.selector_nav_bar_item_ico));
return drawable;
}
You can use it like shown below
yourImageView.setImageDrawable(getTintedDrawable(R.drawable.ic_vector_image));