2

I am trying to programmatically add a row to a table which is defined in XML (this row has internal table in it).

<TableLayout android:id="@+id/maintable"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:stretchColumns="*" xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow android:id="@+id/tabrow">
        <TableLayout android:id="@+id/tableLayout1"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <TableRow>
                <TextView android:id="@+id/titlerow" android:layout_height="fill_parent" android:text="Hello world"
                    android:gravity="center" android:layout_width="fill_parent"
                    android:layout_weight="1" android:textSize="17sp"></TextView>
            </TableRow>
            <TableRow>
                <LinearLayout android:orientation="horizontal" 
                    android:layout_width="fill_parent" android:layout_height="fill_parent"
                    android:layout_weight="1">
                    <TextView android:text="@string/time1" android:id="@+id/time1" style="@style/timeview"></TextView>
                    <TextView android:text="@string/time2" style="@style/dayview"></TextView>
                    <TextView android:text="@string/time3" style="@style/dayview"></TextView>

                </LinearLayout>
            </TableRow>

        </TableLayout>

    </TableRow>

Now I want to just add this(tabrow) row many times in the table.

How can I do it ?

Arjit
  • 565
  • 9
  • 27
  • You may want to see my question from a long time ago: http://stackoverflow.com/questions/5342121/inflate-a-view-layout-into-another-layout – Kevin Parker Sep 12 '11 at 18:17
  • @kevin I tired doing your way View menuLayout = inflater1.inflate(R.id.timealarm, mainLayout, true); This line is giving error – Arjit Sep 12 '11 at 20:32

1 Answers1

2

something like this should do:

// get your table layout
TableLayout tl = (TableLayout) findViewById(R.id.WhateverYoursIs);

// Create new row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

// Create textview
TextView t = new TextView(this);
//
// configure your textview's and do this 2 more times  
//

t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

// add textview to row
tr.addView(t);
//
// do this 2 more times  
//

// add row to table
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

you just have to put the row-creation-stuff in a loop depending on how often you need it

DonGru
  • 13,532
  • 8
  • 45
  • 55