Reputation: 123
I have created a tablelayout in xml but am adding tablerows dynamically. I thought I had it, but when executed I only get the first tablerow added.
TableRow componentDTableRow() {
Log.d("ADD TO D", "TABLE D");
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(2, 2, 2, 2);
TableRow newRow=null;
for (int x = 0; x < 10; x++) {
newRow = new TableRow(this);
for (int y = 0; y < 10; y++) {
TextView textViewD = new TextView(this);
textViewD.setTextSize(18);
textViewD.setWidth(400);
textViewD.setTextColor(Color.BLACK);
String test = "TEST";
textViewD.setText(test);
newRow.addView(textViewD, params);
}
}
return newRow;
}
Part of the xml:
<ScrollView
android:id="@+id/svD"
style="@android:style/Widget.ScrollView"
android:layout_width="791dp"
android:layout_height="272dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/vsC"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/hsvB">
<HorizontalScrollView
android:id="@+id/hsvD"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/tableD"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
The scollview is in a constraintlayout. I figure it's something in the loops but I can't determine what. Any help appreciated.
Upvotes: 0
Views: 108
Reputation: 8237
1.add TableLayout
2.for loop to add newRow
3.TableLayout
addView
in for loop
You can do like this .
Log.d("ADD TO D", "TABLE D");
// add TableLayout
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableD);
// add TableRow.LayoutParams
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(2, 2, 2, 2);
TableRow newRow = null;
// for loop to add newRow
for (int x = 0; x < 10; x++) {
newRow = new TableRow(this);
for (int y = 0; y < 10; y++) {
TextView textViewD = new TextView(this);
textViewD.setTextSize(18);
textViewD.setWidth(400);
textViewD.setTextColor(Color.BLACK);
String test = "TEST";
textViewD.setText(test);
newRow.addView(textViewD, params);
}
// tableLayout addView
tableLayout.addView(newRow);
}
Upvotes: 1
Reputation: 86948
You are simply overwriting newRow
10 times:
for (int x = 0; x < 10; x++) {
newRow = new TableRow(this);
for (int y = 0; y < 10; y++) {
//etc
}
// Here you need to do something with `newRow`, perhaps add it to your table
}
Upvotes: 0