abdfahim
abdfahim

Reputation: 2553

Text color selector for recyclerview

I want to assign a text_color_selector to my textview. Initially when I do that with android:textColor="@drawable/list_selector_nav_drawer_text", it works fine (unpressed text color is black). But when I use code below, unpressed text color becomes purple (similar to the color of visited links in HTML)! What am I doing wrong :( ?

I am using recyclerview.

public void removeNavItemSelected(View v){
        if(v!= null) {
            TextView tview;
            tview = (TextView) v.findViewById(R.id.title);
            tview.setTextColor(R.drawable.list_selector_nav_drawer_text); // Why on this earth color becomes purple rather than black !!!
        }
}

list_selector_nav_drawer_text

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:color="@color/blue" >
    </item>

    <item android:color="@color/black" >
    </item>

</selector>

Upvotes: 2

Views: 1699

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

The above code

setTextColor(R.drawable.list_selector_nav_drawer_text)

will translate to an int and therefore to a random number in memory which ever it was assigned and the setTextColor will see it as color not a color state list.

what you need to do is to place the list_selector_nav_drawer_text xml selector in your color resource folder and call the context instance from your activity to get the statelist.

sample:

//xml should be in the color resource folder    
tview.setTextColor(context.getResources().getColor(R.color.list_selector_nav_drawer_text));

Upvotes: 2

Related Questions