whoatenine
whoatenine

Reputation: 92

Alternate Background Color on a TableRow

I have a TableView that adds a TableRow for each entry in an XML file that it parses from.

To set the background color, I am currently using:

TableRow.setBackgroundResource(color);

How can I set it so each TableRow alternates background colors?

Upvotes: 1

Views: 3956

Answers (1)

sahhhm
sahhhm

Reputation: 5365

A simple and straightforward solution could be, as you iterate over each entry in the XML file, keep a counter. If the counter is odd, use color1, if the counter is even, use color2.

for(int i = 0; i < NUM_XML_ENTRIES; i++){
  // add table row
  if (i % 2) {
    TableRow.setBackgroundResource(color1);
  } else {
    TableRow.setBackgroundResource(color2);
  }
}

Upvotes: 3

Related Questions