Tablerow and TableLayout not aligning straight

32 Views Asked by At

data_shifted_down

Im creating an android project(java) that pulls data from server and places that data into a table so I have to create a table programmatically. I followed a few examples here but for every column in row its being shifted down... I attached a pic.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TableLayout
    android:id="@+id/details_table"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    >

    <TableRow>
        <TextView
            android:text="User"
            android:layout_width="match_parent"
            android:layout_column="0"
            android:layout_weight="1"
            android:textSize="50px"
            android:textColor="@color/black"  />
        <TextView
            android:text="Store"
            android:layout_width="match_parent"
            android:layout_column="1"
            android:layout_weight="1"
            android:textSize="50px"
            android:textColor="@color/black"  />
        <TextView
            android:text="Item"
            android:layout_width="match_parent"
            android:layout_column="2"
            android:layout_weight="1"
            android:textSize="50px"
            android:textColor="@color/black" />
        <TextView
            android:text="QTY"
            android:layout_width="match_parent"
            android:layout_column="3"
            android:layout_weight="1"
            android:textSize="50px"
            android:textColor="@color/black"
            />
    </TableRow>

</TableLayout >
</LinearLayout>

table_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <TableRow
        android:id="@+id/tr"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <TextView
            android:id="@+id/tableCell1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_column="0"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/tableCell2"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_toRightOf="@+id/tableCell1"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/tableCell3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_column="2"
            android:layout_toRightOf="@+id/tableCell2"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/tableCell4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_column="3"
            android:layout_toRightOf="@+id/tableCell3"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textSize="20dp" />
    </TableRow>
</RelativeLayout>

MainActivity.java where it iterates through the data to add to TableRow then add that TableRow into TableLayout:

for (int c = 0; c < a.length; c++) {
    final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
    View v=getLayoutInflater().inflate(R.layout.tablerow, null);
    TableRow tableRow = (TableRow) v.findViewById(R.id.tr);
    detailsTable.setStretchAllColumns(true);
    TextView tv = null;
    switch (c) {
        case 0:
            tv = (TextView) tableRow.findViewById(R.id.tableCell1);
            break;
        case 1:
            tv = (TextView) tableRow.findViewById(R.id.tableCell2);
            break;
        case 2:
            tv = (TextView) tableRow.findViewById(R.id.tableCell3);
            break;
        case 3:
            tv = (TextView) tableRow.findViewById(R.id.tableCell4);
            break;
    }
    tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
    tv.setText(a[c]);
    tv.setBackgroundColor(Color.parseColor("#f8f8f8"));
    tv.setLayoutParams(new
            TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.MATCH_PARENT));
    //new Log("here: ", Integer.toString(tv.getId()));
    //Add row to the table
    if(tableRow.getParent() !=null){
        ((ViewGroup)tableRow.getParent()).removeView(tableRow);
    }
    detailsTable.addView(tableRow);

Tried every solution on the site. Changed to RelativeLayout no luck. Tried gravity and every other attribe...

1

There are 1 best solutions below

0
Andrew On

Looks a horrible way to try and do it but your problem is you are inflating a new tablerow for each item of data with:-

View v=getLayoutInflater().inflate(R.layout.tablerow, null);

who's parent is null

therefore:-

if(tableRow.getParent() !=null){ ((ViewGroup)tableRow.getParent()).removeView(tableRow); }

won't remove a view (not that you should be removing a view)

not tested but something like this might work but for only where a has 4 data items (because you switch statement won't work for anything longer - could also use modulus calculation for that)

View v;
for (int c = 0; c < a.length; c++) {
  // Every 4th data item (remainder is zero) e.g. 0,4,8,etc
  if ((c%4) == 0) {
    // inflate the view
    v=getLayoutInflater().inflate(R.layout.tablerow, null);
  }
  // Rest of your code
  ....
  //Add row to the table
  if ((c%4) == 0) {
    detailsTable.addView(tableRow);
  }
}