I’m develop a dynamic table layout using java code
the first row is in xml
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/TableMain" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dip"
android:background="#000000" >
<LinearLayout
android:id="@+id/lTable11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:layout_weight="2"
android:background="#C0C0C0"
android:text="Grade"
android:gravity="right"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:layout_weight="2"
android:background="#C0C0C0"
android:text="Hourse"
android:gravity="right"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:layout_weight="6"
android:background="#C0C0C0"
android:text="Subject Name"
android:gravity="right"
android:padding="5dp"
/>
</LinearLayout>
</TableRow>
</TableLayout>
the remain rows is in the java part which is have the same property in xml
TextView tvRowGrades[]=new TextView[gotSubName.length];
TextView tvRowHourse[]=new TextView[gotSubName.length];
TextView tvRowSubName[]=new TextView[gotSubName.length];
TableRow row[]=new TableRow[gotGradeItem.length];
LinearLayout LRow[]=new LinearLayout[gotGradeItem.length];
tableMain=(TableLayout)findViewById(R.id.TableMain);
for(int i=0;i<gotHourse.length;i++){
row[i]=new TableRow(this);
TableRow.LayoutParams lRow=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
lRow.setMargins(2, 2, 2, 2);
row[i].setLayoutParams(lRow);
row[i].setBackgroundColor(Color.BLACK);
LinearLayout.LayoutParams lpLRow=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LRow[i]=new LinearLayout(this);
LRow[i].setLayoutParams(lpLRow);
LRow[i].setGravity(Gravity.CENTER);
LRow[i].setOrientation(LinearLayout.HORIZONTAL);
tvRowGrades[i]=new TextView(this);
tvRowGrades[i].setText(gotGradeItem[i]);
android.widget.LinearLayout.LayoutParams lpTvOther=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.FILL_PARENT, 2);
lpTvOther.setMargins(1, 1, 1, 1);
tvRowGrades[i].setPadding(5, 5, 5, 5);
tvRowGrades[i].setGravity(Gravity.RIGHT);
tvRowGrades[i].setLayoutParams(lpTvOther);
LRow[i].addView(tvRowGrades[i]);
tvRowHourse[i]=new TextView(this);
tvRowHourse[i].setText(gotHourse[i]+"");
tvRowHourse[i].setPadding(5, 5, 5, 5);
tvRowHourse[i].setGravity(Gravity.RIGHT);
tvRowHourse[i].setLayoutParams(lpTvOther);
LRow[i].addView(tvRowHourse[i]);
tvRowSubName[i]=new TextView(this);
tvRowSubName[i].setText(gotSubName[i]);
android.widget.LinearLayout.LayoutParams lpTvSubName=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.FILL_PARENT, 6);
lpTvSubName.setMargins(1, 1, 1, 1);
tvRowSubName[i].setPadding(5, 5, 5, 5);
tvRowSubName[i].setGravity(Gravity.RIGHT);
tvRowSubName[i].setLayoutParams(lpTvSubName);
LRow[i].addView(tvRowSubName[i]);
row[i].addView(LRow[i]);
tableMain.addView(row[i]);
}
When i run the application the table is not displaying
please i want solve this problem
change the
TableRowwidth to match_parentThe problem is that your row’s width is defined by it’s content (
wrap_content), and your content’s width (theLinearLayoutthat is inside the row) is defined by the parent.You can also delete the
LinearLayoutand put theTextViewitems directly in theTableRowas it extendsLinearLayout.For the other rows that you add in the code you should also change the
LayoutParams: