H.Android
H.Android

Reputation: 198

android listview items background

I needed to change the background of listview items when normal and when selected..

I used:

    <ListView
    android:id="@+id/resultLV"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"        
    android:listSelector="@drawable/list_selector"        
    android:scrollbars="none" />

Where list_selector.xml is:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

 <!-- Selector style for listrow -->
 <item android:drawable="@drawable/listoff" android:state_pressed="false"   
 android:state_selected="false"/>
 <item android:drawable="@drawable/liston" android:state_pressed="true"/>
 <item android:drawable="@drawable/listoff"/>

 </selector>

The problem is, when the activity starts, the background of listview items is not changed to listoff background.. But when i select an item, it correctly changes to liston background and when released it changes to listoff background..

So my problem is at start, why it doesn't appear with listoff background drawable?

Thanks

Upvotes: 0

Views: 101

Answers (3)

Atul O Holic
Atul O Holic

Reputation: 6792

Add the selector to the ListView item instead of ListView itself, as when there are clickables with a ListItem the click event responds to the Items first. :)

You can verify this by adding logs in onClick method of the ListView item and the onItemClick of ListView and you will notice that onClick event will be called skipping the onItemClick of ListView.

Glad to help you. :)

Upvotes: 1

Sethu
Sethu

Reputation: 428

try this one..

<!-- Tab widget design -->
<item android:drawable="@drawable/listpres" android:state_pressed="true" android:state_selected="false"></item>
<item android:drawable="@drawable/list" android:state_pressed="false" android:state_selected="false"></item>
<item android:drawable="@drawable/listpres" android:state_selected="true"></item>

Upvotes: 0

Rajith Rajan
Rajith Rajan

Reputation: 121

The problem on your xml selector drawable.Instaed of gradiant color please put your drawable image.

Please have look @ this drawables

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:dither="true">

  <item android:state_pressed="true"><shape>
  <gradient android:angle="0" android:endColor="#0080FF" android:startColor="#0080FF" />        </shape></item>
    <item><shape>
      <gradient android:angle="0" android:endColor="#3B5998" android:startColor="#3B5998" />


        </shape></item>

</selector>

Hope this will help you

Upvotes: 0

Related Questions