views:

39

answers:

1

I'm trying to have a ListView that will display each row in a given color, depending on the content of that row, but still allow the selector to draw. As it is now, if I call View.setBackgroundColor on a row, it colors the background properly, but the selector isn't drawn.

+1  A: 

You have three major options.

One approach is to use android:drawSelectorOnTop="true" and set the selector of your ListView to be something that is transparent/translucent, so when it is drawn over top of a row, you can still see the row contents.

Or, you will need to set the row backgrounds not to a color, but to a StateListDrawable, where the background color is set to be transparent when the row is selected, so the regular selector (drawn behind the row) is visible.

You are also welcome to register a selection listener with the ListView and do custom color changes based on when the selection changes.

CommonsWare
I've tried to use android:drawSelectorOnTop="true" but it ends up drawing over the entire View.I've also tried using a StateListDrawable, but it ends up removing the background on like-colored rows when a row is selected and the state of the drawable changes.Maybe it's because I'm using a complex view, with multiple text views and a checkbox, instead of a simple TextView, where I've seen these methods work.
cmccurdy
"I've tried to use android:drawSelectorOnTop="true" but it ends up drawing over the entire View." -- which is why it has to be transparent/translucent, as I noted in my answer.
CommonsWare
Thought I had attempted that yesterday, but apparently it works today. Thanks for the help.
cmccurdy