views:

4111

answers:

5

I have a ListView that I'm populating from a custom ListAdapter. Inside the Adapter (in the getView(int, View, ViewGroup) method) I'm setting the background color of the View using setBackgroundColor(int). The problem is that no matter what color I set the background to it always comes out a dark grey. It might also be worth noting that I'm using the Light theme.

Relevant (simplified) bits of code:

AndroidManifest.xml:

<activity 
    android:name=".MyActivity"
    android:theme="@android:style/Theme.Light" />

MyAdapter.java:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View av = inflater.inflate(R.layout.my_row, parent, false);
    av.setBackgroundColor(R.color.myRow_red);
    mName = (TextView) av.findViewById(R.id.myRow_name);
    mName.setText("This is a name");
    return av;
}

Any ideas/suggestions?

+2  A: 

You must set the cacheColorHint attribute to the desired background color for your list. This is a required workaround to account for a drawing optimization Android performs on lists.

See here: link text

Matthias
That's right, and if you don't need to change your background color dynamically, you can just add android:background="#FF0000" in your my_row.xml layout file.
Dimitar Dimitrov
midj: that's actually not true in my case, you're just seeing a super-simplified example.
fiXedd
@fiXedd: so what's your problem then? Didn't deactivating the color cache help?
Matthias
+4  A: 

You should use setBackgroundResource(R.color.myRow_red) instead of setBackgroundColor(). In your example background color is assigned with the ID instead of the actual color described in the resources.

Max Feldman
A: 

You cold always wrap the whole row inside another view and set the background color on that view. This view would be the first (and only) child of the row.

PHP_Jedi
A: 

try this:

setBackgroundColor(0xFF5DB9FB);

Laces
A: 

Try to do like this:

av.setBackgroundColor(getResources().getColor(R.color.myRow_red));

Rodrigo