Jeremy Logan
Jeremy Logan

Reputation: 47514

Android ListView background colors always showing grey

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?

Upvotes: 9

Views: 23386

Answers (6)

Max Feldman
Max Feldman

Reputation: 326

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.

Upvotes: 21

Alex
Alex

Reputation: 21

I ran into a similar problem where the divider was coming up grey. I found that the other solutions had no effect, but android:divider="<drawable,color, or whatever>" on my ListView worked.

Upvotes: 2

Laces
Laces

Reputation: 1

try this:

setBackgroundColor(0xFF5DB9FB);

Upvotes: 0

Rodrigo
Rodrigo

Reputation: 1

Try to do like this:

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

Upvotes: 0

Vidar Vestnes
Vidar Vestnes

Reputation: 42964

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.

Upvotes: 0

mxk
mxk

Reputation: 43524

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

Upvotes: 6

Related Questions