I am making a navigation drawer where the icon is coloured based on the colour of the text.
This is my selector
declared in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@color/emerald"/>
<item android:state_selected="true" android:color="@color/emerald"/>
<item android:state_pressed="true" android:color="@color/emerald"/>
<item android:color="@android:color/white"/>
</selector>
This is my ViewHolder
Drawable drawable = ContextCompat.getDrawable(mContext,iconResourceId);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(drawable.mutate(),mContext.gerResouces.getColorStateList());
mItemIcon.setImageDrawable(drawable);
As you can see the problem i am having is on this line, what do i pass in getColorStateList
? The doucmentation is not helping me.
DrawableCompat.setTintList(drawable.mutate(),mContext.gerResouces.getColorStateList());
Pass the id of the color state list resource, e.g. R.color.my_color_state_list
. Color state lists belong in res/color
, not res/drawable
.
DrawableCompat.setTintList(drawable.mutate(),
mContext.getResources().getColorStateList(R.color.my_color_state_list));
ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.your_color_selector);
snackBar.setActionTextColor(colorStateList);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With