user1166635
user1166635

Reputation: 2801

How can I set selector for my ListView?

I have following problem: my application has layout with white background and ListView. If I need to scroll this ListView then it changes color for black! I have just made selector the for ListView, but it doesn't work while. My ListView:

        <ListView
            android:id="@+id/listViewLastUpdates"
            android:listSelector="@drawable/selector"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

My selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/green" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/green" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/white" /> <!-- default -->
</selector> 

I need to user green color as highlight and white color as background (then if I scroll ListView it won't change a color). Please, tell me, how should I fix it?

Upvotes: 0

Views: 2137

Answers (2)

kiko283
kiko283

Reputation: 530

Try this and tell me if it helped:

<ListView
    android:id="@+id/listViewLastUpdates"
    android:listSelector="@drawable/selector"
    android:background="@android:color/white"
    android:cacheColorHint="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

Upvotes: 1

Vineet Shukla
Vineet Shukla

Reputation: 24031

use color selector instead of drawable selector as you are setting color.

Make color folder inside res and save this color xml selector and use it:

<?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/green" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:color="@color/green" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:color="@color/green" /> <!-- pressed -->
    <item android:color="@color/white" /> <!-- default -->
</selector> 

Upvotes: 1

Related Questions