Android: background listview

Go To StackoverFlow.com

1

How can I change the background of a single cell of a ListView? I wrote in java:

n_list = new ListView(this);
n_list.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_pressed));

This way changes the background of the whole list. When I press on a cell it changes the background of all cells.

list_pressed.xml on drawable

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

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

    <item android:state_focused="true"
          android:drawable="@drawable/listviewpress" />

    <item android:state_pressed="true"
          android:drawable="@drawable/listviewpress" />
    <item 
          android:drawable="@drawable/listview" />
</selector>

How can I fix this? Thanks in advance :)

2012-04-05 01:07
by Tony
check this series of ListView tutorials, go in bottom to top matter. You will be confident enough to build ListView next time - Paresh Mayani 2012-04-06 11:09


2

You need to create a Custom Adapter and in the getView() you can say what color you want to give to your row.

If you follow the tutorial on the link you can do on your convertView a setBackground() with the color you want. If you want a specific position just do an if before that setBackground .

2012-04-05 01:37
by Tiago Almeida


1

Below are few URL to have listview with customadapter so that you can set background to rows in getVIew()::

LINL1

LINK2

LINK3

LINK4

LINK5

2012-04-05 02:21
by Shankar Agarwal
Ads