Vishnu Srivastava
Vishnu Srivastava

Reputation: 3

How to assign different colors for each list item in the list view?

I have a list view in which I am loading my own layout. I am changing the color of the custom view but its not refleting.

How to do this?

Upvotes: 0

Views: 264

Answers (3)

anddev
anddev

Reputation: 3204

Hello If you want to use any two colors then you can use below code into your customAdapter.

    private int[] colors = new int[] { Color.LTGRAY, Color.WHITE };

And in getView() write

    int colorPos = position % colors.length;
    convertView.setBackgroundColor(colors[colorPos]);

I hope it helps you.

Upvotes: 0

Noby
Noby

Reputation: 6602

Try this...

In your custom layout recognize your root element and set its background color.

linearLayout.setBackgroundColor(Color.WHITE);

Upvotes: 1

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

You will have to create custom adapter, and configure individual views as you like. Keep in mind, that list views can be recycled for better performance

Upvotes: 0

Related Questions