Android: How to get ColorStateList from resources?

I create a navigation box where the icon is colored depending on the color 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 facing is on this line, what am I passing to getColorStateList? Ocularization does not help me.

DrawableCompat.setTintList(drawable.mutate(),mContext.gerResouces.getColorStateList());
+4
source share
2 answers

Pass the resource identifier of the color state list, for example. R.color.my_color_state_list. Color state lists belong res/color, not res/drawable.

DrawableCompat.setTintList(drawable.mutate(),
    mContext.getResources().getColorStateList(R.color.my_color_state_list));
+2
source
ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.your_color_selector);
snackBar.setActionTextColor(colorStateList);
+3
source

All Articles