views:

284

answers:

3

Hi,

I've created an inbox Activity and I'm mirroring some functionality of the default Mail application that comes with Android.

In Mail, the background color of a message that has not been read is a lighter color than the rest of the items in the list. I've mirrored this by setting calling setBackgroundResource in the getView method of my Adapter. setBackgroundColor doesn't do anything.

        if (!msgSum.getIsRead()) {
            LinearLayout l = (LinearLayout) v.findViewById(R.id.inbox_background);
            if (l != null) {
                l.setBackgroundResource(R.color.inbox_unread);
            }
        }
        else {
            LinearLayout l = (LinearLayout) v.findViewById(R.id.inbox_background);
            if (l != null) {
                l.setBackgroundResource(R.color.inbox_read);
            }
        }

The problem is, the items then lose the ability to highlight. What I mean is, typically if you scroll your mouse wheel while using the emulator (or if your Android device has some sort of scrolling capability, you use that), the list items will turn yellow like they have focus.

How do I accomplish what I'm trying to do without losing the ability to highlight a list item?

Thanks for your help

+3  A: 

This is because the highlight is drawn behind the list items. You need to create a state list drawable for your item's background that sets the background color to transparent in the selected state. If you lookup the talk about ListView I gave at Google I/O 2010, you will get an example of how to do this.

Romain Guy
I'll have a look. I'm watching your "Google I/O 2010 - The world of ListView" video on YouTube right now. Thanks for the direction
Andrew
I do believe 31:43 of that video is what I need. Where should that xml file live? res/selector/myselector.xml?
Andrew
I have everything working nicely. Thanks for your help and taking the time to help the Android community.
Andrew
+1  A: 

Per Romain Guy's suggestion, I watched the "Google I/O 2010 - The world of ListView" video on YouTube. Around minute 31:43, he posts a snippet of code that solves this problem. You must make a couple modifications, though (I'm developing against 2.1). First you must declare the xmlns:android parameter in the selector tag. In your Java code, you must call convertView.setBackgroundResource(R.drawable.yourselector), instead of setBackground (which no longer appears to exist).

Andrew
A: 

This was very helpful. Andrew Thanks for pointing the location in the video.

Prashant