views:

26

answers:

1

I have a need to generate a table layout dynamically using code. I can't get it to display. I've reduced the table generation code segment down to its basics to try and nut it out but even this greatly simplified version isn't working (ie. doesn't display). Can someone please point out what I'm doing wrong? Thanks.

public class TableByCodeTest extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setLayoutParams(
        new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    TableRow firstTableRow = new TableRow(this);
    firstTableRow.setLayoutParams(
        new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    TextView title = new TextView(this);
    title.setText(R.string.title);
    title.setLayoutParams(
        new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

    firstTableRow.addView(title);
    tableLayout.addView(firstTableRow);
    setContentView(tableLayout);

    }
}
A: 

Change the line:

firstTableRow.addView(title);

to

firstTableRow.addView(title,new TableRow.LayoutParams(0));

To add it to row 0.

Cpt.Ohlund
Ah! That works. Many thanks!
ianww