0

how to set layout_span, layout_margin dynamically in code in android

Amit
  • 11
  • 2
  • 4
  • This problem was simple solved here: http://stackoverflow.com/questions/4637233/how-to-set-layout-span-through-code – Derzu Feb 13 '12 at 12:05

1 Answers1

6

You can set layout_margin with a TableRow.LayoutParams instance given to TableRow.addView():

TableLayout newTable = new TableLayout(DisplayContext);
int rowIndex = 0;

// First Row (3 buttons)
TableRow newRow1 = new TableRow(DisplayContext);
newRow1.addView(new Button(DisplayContext), 0);
newRow1.addView(new Button(DisplayContext), 1);
newRow1.addView(new Button(DisplayContext), 2);
newTable.addView(newRow1, rowIndex++);

// Second Row (2 buttons, last one spans 2 columns)
TableRow newRow2 = new TableRow(DisplayContext);
newRow2.addView(new Button(DisplayContext), 0);
Button newButton = new Button(DisplayContext);

// Create the layout to span two columns
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 2;
newRow2.addView(newButton, 1, params);

newTable.addView(newRow2, rowIndex++);
Jeff Hay
  • 2,655
  • 28
  • 32