tags:

views:

88

answers:

1

hi guys,

I am trying to add TableLayout to the LinearLayout from resourse(xml) using programmable way.

The added TableLayout count is dynamic. It would be between 1 - 10. I think it is better way to make it from resource xml. Because design doesn't break.

How to make it ?

Actually I don't know how to create a new instance from the XML.

Please help. guys.

Thanks in advance.

+2  A: 

Something like this:

TableLayout table = (TableLayout)findViewById(R.id.table);

LayoutInflater inflater = getLayoutInflater();

for(int i = 0; i < 10; i++) {
    TableRow row = (TableRow)inflater.inflate(R.id.table_row, 
        table, false);

    TextView text = (TextView)row.findViewById(R.id.text);
    text.setText("row: " + i);
    // other customizations to the row

    table.addView(row);
}

Where table_row.xml looks something like this:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <TextView android:id="@+id/text" />
    <TextView android:id="@+id/text2" />
</TableRow>

This way you can still create the table row in XML, and have the number of rows be dynamic.

synic
Great' Thanks Synic