I'm trying to dynamically add rows to my table layout in Android (I referred to this tutorial: http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout). However, it's leading to an incorrect view hierarchy. I've attached the code as well as an output image.
XML File:
<TableLayout
android:id="@+id/task_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:id="@+id/task_heading_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/task_heading_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="Task"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="1"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="2"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="3"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="4"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="5"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="6"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="7"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="8"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
</TableRow>
</TableLayout>
Java Code:
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView t1 = new TextView(this);
t1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
String heading = "heading";
t1.setText(heading);
row.addView(t1);
for (int i=0;i<8;i++){
TextView view = new TextView(this);
view.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
view.setText("child");
row.addView(view);
}
task_table.addView(row, new TableLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
In the output all the 8 views with text "child" I'm adding in the loop get added to the column titled 1 instead of one "child" per column.
Expected output:
Task | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
heading | child | child | child | child | child | child | child | child
Actual output:
Task | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
heading | child child child child child child child child | | | | | | |
Can someone please help me and let me know what I'm doing wrong? Any help will be greatly appreciated.