in XML I've definded a TableLayout with a TableRow (the headline of the table). Every other row will be added programmatically. The problem: I can't get the new rows to line up with the one of the XML
- 3 TextView Array, 1 TableRow Array
- Loop to fill these arrays with TextViews and TableRows
- All are given to the setTableRowParams method
- Setting text for the TextViews
Adding all rows to the TableLayout
driversNames = new TextView[DRIVERSAMOUNT]; driversDriven = new TextView[DRIVERSAMOUNT]; driversRemaining = new TextView[DRIVERSAMOUNT]; driversRows = new TableRow[DRIVERSAMOUNT]; for (int i = 0; i < DRIVERSAMOUNT; i++) { driversNames[i] = new TextView(this); driversDriven[i] = new TextView(this); driversRemaining[i] = new TextView(this); driversRows[i] = new TableRow(this); setTableRowParams(driversRows[i]); driversNames[i].setText(driversArray[i].getName()); driversDriven[i].setText("" + driversArray[i].getTimeDriven()); driversRemaining[i].setText("" + driversArray[i].getTimeRemaining()); driversRows[i].addView(driversNames[i]); driversRows[i].addView(driversDriven[i]); driversRows[i].addView(driversRemaining[i]); tableLayout.addView(driversRows[i]); }
setTableRowParams() Method
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT); tableRowParams.setMargins(10, 10, 10, 10); tr.setLayoutParams(tableRowParams);
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_driver" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:elevation="10dp" card_view:cardCornerRadius="2dp"> <TableLayout android:id="@+id/tableDrivers" android:layout_width="match_parent" android:layout_height="wrap_content"> <TableRow android:id="@+id/driversRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tVDriversTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_span="3" android:layout_weight="1" android:text="@string/card_drivers" android:textAppearance="?android:attr/textAppearanceLarge" /> </TableRow> <TableRow android:id="@+id/driversRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tVDriversName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/drivers" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tVDriversDriven" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/timeDriven" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tVDriversRemaining" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/timeLeft" android:textAppearance="?android:attr/textAppearanceMedium" /> </TableRow> <!-- THE VALUES ARE ADDED PROGRAMATICALLY --> </TableLayout> </android.support.v7.widget.CardView>
This is the result. As you can see the rows Driver 1-4 don't line up with the headline. Sorry for the foreign language ;-)
Can you please tell me where my mistake is?