Search code examples
androidandroid-layoutandroid-tablelayout

Weight of programmatically added elements in TableRow doesn't work


So, I have a TableLayout with 4 TableRow in one xml file. I also have a xml file defining a TextView that will be inserted in the row (3 TextView per row). I'd like those TextView to have the same width and that was working well when everything was in the same xml file (at the begining my TextView were "hard-coded") but now that I add them programmatically, they don't have the same width.

Here is the first XML file

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/homeTable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/lmbg"
    android:clickable="true"
    android:padding="20dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:minHeight="200dp" >
    </TableRow>

</TableLayout>

This is my TextView item

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_weight="1"
    android:clickable="true"
    android:gravity="center"
    android:onClick="goToList"
    android:textSize="14sp" />

And finally this is how I add them programmatically

for(int i = 0; i < mTopCategories.size(); i++){     
    Category c = mTopCategories.get(i);
    TableRow row = ((TableRow)findViewById(rowID[idIdx])) ;
    cat = (TextView) getLayoutInflater().inflate(R.layout.cat_grid_item, null);
    int iconID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "drawable", getPackageName());
    cat.setCompoundDrawablesWithIntrinsicBounds(0, iconID, 0, 0);
    int textID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "string", getPackageName());
    cat.setText(textID);
    cat.setTag(c.getId().toString());
    row.addView(cat);
    }

Solution

  • Try explicitly setting the layout params on the TextViews like this:

    TableRow.LayoutParams lParams = new TableRow.LayoutParams(0, -2 /* WRAP_CONTENT */, 0.33f /* 33% of width */);
    cat.setLayoutParams(lParams);
    

    See http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#TableRow.LayoutParams%28int,%20int,%20float%29 for more info.