sponturious
sponturious

Reputation: 131

Trying to set background color of a cell(row/col) in GridView Android

how do i set a custom color for a row/col in gridview in android?

My gridView consists of a gridArray of type Items which I pass all the data I want it to display, its for a timetable. I calculate the position of the items based on where I want them and then Display the items. I am not able to change the background color of the cell. How would I go about doing that.

for (int d1 = 0; d1 < days.size(); d1++) {
            if (myModules.get(1).equals(days.get(d1))) {
                for (int e1 = 0; e1 < times.size(); e1++) {
                    if (startEndM1[0].equals(times.get(e1))) {
                        gridArray.set(((e1 * 5) + d1),new Item(myModules.get(0)));
                        if (!startEndM1[1].equals(times.get(e1 + 1))) {
                            gridArray.set((((e1 * 5) + d1) + 5), new Item(myModules.get(0)));                           
                        }
                        int position = ((e1 * 5) + d1);
                        ((View) gridView.getItemAtPosition(position)).setBackgroundColor(Color.BLUE);
                    }
                }
            }
        }

the above code is justto set an item onto its specified position, If I have to change the color of that particular backgorund cell where I am setting the item, it crashes. The Grid view should recognize the position of the item where its at. Some help would be really apprectiated, thanks.

Upvotes: 3

Views: 7798

Answers (1)

Matej Špil&#225;r
Matej Špil&#225;r

Reputation: 2687

ok first of all, i made a crossword game and also had to change background color of cell ... i made it this way

View tv = (View) gridView.getChildAt(i);
tv.setBackgroundColor(Color.RED);

also anothers links that helped me a lot were :

Change Color of cell of grid

http://eureka.ykyuen.info/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/

Upvotes: 3

Related Questions