3

I have a question about list views. I hope someone knows the solution, because I have been trying to solve this problem all weekend.

I have a custom list view in which every row have a custom .xml (item_row.xml) with a set color background for the row item.

<?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="70dip"
  android:orientation="vertical"
  android:background="@drawable/list_bg">

The problem is that when I try to make a selector for the list. I want two things:

  • When the item is focused, the typical orange rectangle should appear over it,
  • and the same When the item is pressed.

I have proved with a selector and a custom style but all I get is or the selector doesn't appear or the selector covering the item so I can't see it.

If anyone could help me with finding the right code I would be very grateful.

Thanks

David Thomas
  • 249,100
  • 51
  • 377
  • 410
Gerardo
  • 5,800
  • 11
  • 66
  • 94

2 Answers2

6

Add an attribute to your ListView tag.

android:listSelector="@drawable/list_bg"

Now you can create your own style for your background using the XML file as drawable. For more information, check out the API Demos app, drawables section.

Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
Praveen
  • 90,477
  • 74
  • 177
  • 219
3

There is a special parameter for listview selected backgronud, i.e. android:listSelector. Setting this parameter to the background resource will solve your problem.

Karan
  • 12,724
  • 6
  • 40
  • 33