Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I get a ColorStateList from resources?

Tags:

android

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());
like image 999
Ersen Osman Avatar asked Sep 01 '15 15:09

Ersen Osman


2 Answers

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));
like image 111
Karakuri Avatar answered Nov 11 '22 18:11

Karakuri


ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.your_color_selector);
snackBar.setActionTextColor(colorStateList);
like image 28
peuhse Avatar answered Nov 11 '22 18:11

peuhse