1

I am developing one app and in that i have one listview. In listview i want to change color of selected items only. It means if i clicked on 1st item it should change color and then again i click on 2nd item then color of 1st item will become normal and it will change color of 2nd item. Here i am using custom listview. and here position is selected item and CommonUtilities.getListPosition() is globally defined method for storing position. I am able to change color on select but when i click on 2nd item color doesn't change to its previous color.

if (position == CommonUtilities.getListPosition()) {
    v.setBackgroundColor(Color.CYAN);
}else{
    v.setBackgroundColor(Color.WHITE);
}
Saurabh
  • 457
  • 2
  • 8
  • 26

2 Answers2

0

ListView item background via custom selector. This link will help you. Use a selector. Specify different color when pressed and in normal mode. I hope this is exactly what your looking for.

when you click a row in listview u can change the color of the listview on click. Then on release change back to normal state. Why do you want to change the 1st row color when you click 2nd row?. Each row in listview remains in normal state until it is clicked. On release goes back to normal state. The above link helps you achieve the same.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • as i edited my question, i am able to change color on itemclick. but as i click on 2nd item 1st item should move to its original color. i have used selector for changing color. – Saurabh Feb 12 '13 at 06:33
  • Just like how button works. In selector.xml android:state_selected="true" android:drawable="@drawble/pressed". Define pressed.xml in drawable folder. In pressed.xml define what background you want. The above link is a good place to start. – Raghunandan Feb 13 '13 at 06:39
  • What do you mean by release view? – Raghunandan Feb 13 '13 at 06:39
0

Apply the listSelector in the xml in the ListView like this:

android:listSelector="@drawable/list_selector"

And create list_selector.xml file in drawable folder with following content:

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

    <item android:drawable="@color/row_not_selected" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@color/row_selected" android:state_pressed="true" android:state_selected="true"/>

</selector>

and create two colors in the colors.xml with names row_not_selected, row_selected.

Thats it.Thanks. Please let me know if anything else required.

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61