user3241084
user3241084

Reputation: 87

Listview selector wrong background

at API7 there I have the problem that if I select a row the background changes and not my selected row. Where is my mistake? With my phone with Android 4 there is no problem with the color, whats wrong?

list_selector_flatcolor.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/list_row_default_bg" android:state_pressed="false" android:state_activated="true"/>
    <item android:drawable="@color/list_row_pressed_bg" android:state_pressed="true"/>
    <item android:drawable="@color/list_row_selected_bg" android:state_pressed="false" android:state_activated="true"/>

</selector>

Layout with listview:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ListView
        android:id="@+id/custom_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/list_selector_flatcolor"
        android:dividerHeight="1dp"/>

</LinearLayout>

colors.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_color_default">#00000C</color>
    <color name="text_color_inverse">#FFFFFF</color>
    <color name="white">#FFFFFF</color>
    <color name="list_row_default_bg">#ffd30210</color>
    <color name="list_row_pressed_bg">#008cef</color>
    <color name="list_row_selected_bg">#86d3f6</color>
</resources>

enter image description here

Upvotes: 0

Views: 236

Answers (1)

M D
M D

Reputation: 47817

Try this selector.xml. You need to add android:state_selected state.

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

And if you are used Custom Adapter then you can set this selector as a Background to your Custom Layout like:

android:background="@drawable/selector"

Upvotes: 1

Related Questions