1

I am working on an android application. I have created a listview by using

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayname));   
getListView().setTextFilterEnabled(true);   

Now i want to change the selected item's color. I could change the background of the selected item by placing

listviewobject.getChildAt(position).setBackgroundColor(Color.BLACK);

in onListItemClick()

this code is changing the background color but if I select any other list item then also the previously clicked list item's color is red.So I change the previously clicked listitem's color by

l.getChildAt(prevpos).setBackgroundColor(Color.BLACK);

now the problem is if i change the background of previously clicked listitems color to black.Then i can't see the text on that particular listitem.I i click again then only i can see the text on that item.So its look weired.please help me friends

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
sarath
  • 3,181
  • 7
  • 36
  • 58

5 Answers5

19

After going through lots of posts and blogs i found this solution it works for me...

declare row variable global

public View row;

your_list.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> a, View v,
                        int position, long id) {

    if (row != null) {
        row.setBackgroundResource(R.color.orange);
    }
    row = v;
    v.setBackgroundResource(R.color.transparent_green);
)};
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
Vivek Singh
  • 1,201
  • 3
  • 17
  • 39
  • Hey its great. But what to do when we deselect row of listview? I have to keep the same color as previous when row was not selecteted. – Looking Forward Jul 29 '13 at 12:48
  • @AnilBhatiya instead of R.color.transparent_green you can set your listview color. – Vivek Singh Jul 29 '13 at 13:05
  • I tried. but not working. My original background is white. When I write such as arg1.setBackgroundColor(Color.WHITE); it is working but this white color is dark in compare to original background white color. – Looking Forward Jul 29 '13 at 13:08
  • 4
    While this works initially, I have found that it isn't perfect. If your list is big enough to scroll, your background color can get moved to a different row as you scroll. I presume this is because the View is reused for a newly visible row. – Erik Allen Aug 23 '13 at 18:52
  • @ErikAllen - I am also facing the same problem which you have mentioned in your comments. What is the solution for this? When I scroll my list, background color is randomly assigned to a new row. Can help me in fixing it? – KP Joy Feb 19 '17 at 10:31
  • @KPJoy, it has been years and two jobs since I did the change. I believe I solved it by never assuming that the default background colour was set. Always set the background colour to what you want it to be. – Erik Allen Apr 28 '17 at 22:12
5

Well using this in the properties of your list view mught help:

android:listSelector="@drawable/list_selector"

It will automatically set the selected drawable as background of the selected item. Hope this works for you and drawable may be your own choice's color.

Explanation: Add an image strip to your drawables folder/s and set that image in listSelector attribute of the your list view. Now you will navigate through your list view, the list view's background will be of the color of the image strip you set instead of android's native color. Hope you get it now...:-)

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • pls check this...http://stackoverflow.com/questions/9359130/resources-not-found-exception-when-using-xml...now I am getting exception after using selector – sarath Feb 20 '12 at 10:14
  • 2
    well the procedure i told you...in that you don't need to put selector....the attribute I mentioned will automatically perform this all...you just have to make a drawable i.e., an image strip of some color of your choice...remember that will be an image...and set this image to your ListView via listselector attribute.... – Usama Sarwar Feb 20 '12 at 11:36
1

I this This happening because you have put text color as black and your setting the background color also black that's why you can't see the difference. for setting the background color you can use the following line.

view.setBackgroundColor(Color.YELLOW);

use different color then text color.

mayur rahatekar
  • 4,410
  • 12
  • 37
  • 51
  • @sarath Who told you. you are using the array adapter. that's why it is not allowing you.use Base adapter to inflate the listview it will allow to setcolor. The by default color is black so you set some different back ground color list view content can be see. – mayur rahatekar Feb 20 '12 at 07:20
1

By default the background is black. If you have customized your listview then when you scroll it would turn black. So when you define your list view set background cache color to the color you need as below:

    yourlistView.setCacheColorHint(Color.WHITE);
Arun
  • 1,658
  • 1
  • 14
  • 21
0

What you can do is instead of using .setBackgroundColor() with a Color value, create a Color State List and assign it with .setBackgroundResource(). That way, you can define the various states that your list item can become, depending on the item's current state.

seeming.amusing
  • 1,179
  • 1
  • 8
  • 18
  • hi..i write setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayname)); getListView().setBackgroundResource(R.drawable.listcolor); ...But I am getting error ..02-20 14:52:39.011: ERROR/AndroidRuntime(785): java.lang.RuntimeException: Unable to start activity ComponentInfo{pa.gd/pa.gd.freetab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{pa.gd/pa.gd.fThema}: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/listcolor.xml from drawable resource ID #0x7f020012 – sarath Feb 20 '12 at 09:25