I made a grid of buttons dynamically in the program, I used to do this TableLayout , this is my code:
private void createLayoutDynamically() {
won = (TableLayout)findViewById(R.id.won);
for ( int qq = 1; qq < a; qq++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
2
));
won.setPadding(25,25,25,25);
won.addView(tableRow);
for ( int q = 1; q < b; q++) {
myButton = new Button(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
2
));
}
}
}
The result of this code is something like this :
https://i.sstatic.net/Uli6s.jpg
I want to achieve that the background of these buttons change every second like here:
https://i.sstatic.net/sVKXe.jpg Can u help me ?
When filling table rows add to your loop:
for ( int q = 1; q < b; q++) {
myButton = new Button(this);
int drawable = R.drawable.first_button;
if (q%2 == 0) {
drawable = R.drawable.second_button;
}
myButton.setBackground(getResources().getDrawable(drawable)); //you can put Resources to variable and put it outside the loop
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
2));
}