Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, setSelected() and state_selected

I'm having trouble with View.setSelected(). Views are flagged as selected -- TextViews, for example, change their font color -- but my background selectors don't seem to register the change.

Example selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" />

    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#ff8600" />
        </shape>
    </item>
</selector>

I'm not even sure which kind of context information would be useful. The views are children of a LinearLayout, and I'm programatically setting the selected state inside a touch event. As I said, it does seem to work, since the font color goes from white to gray, but the background stays the same.

Edit: I checked for silly mistakes before posting :P. The answer is not "add the android:background attribute".

like image 520
slezica Avatar asked Sep 30 '11 14:09

slezica


1 Answers

The order of items matters in selector xmls, the default item should always be at the bottom in the items list.

like image 63
Ronnie Avatar answered Sep 19 '22 13:09

Ronnie