TableLayout refuses to match_parent

Viewed 436

I have an xml layout which serves as an item in a ListView. The thing is: when it's added there, the item's width isn't stretched to match_parent, it wraps content instead. It is used in a ListView, so its width needs to be match_parent. I've been reading about it all over stackoverflow, trying every solution availible, but I have had zero result: layout_weight set to 1 does nothing to my layout, stretching columns has given me the best result in terms of width, but in terms of layout itself it failed miserably.

Is there anything that can be done here?

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <TableRow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <CheckBox
            android:id="@+id/trackCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:clickable="false"
            android:duplicateParentState="true" />

        <TextView
            android:id="@+id/trackNumberText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="00."
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/trackTitleText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_weight="1"
            android:text="Title"
            android:textColor="@android:color/black"
            android:textSize="18sp" />
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:id="@+id/trackArtistText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_weight="1"
            android:text="Artist" />
    </TableRow>
</TableLayout>
3 Answers

Add android:stretchColumns="*" property in your TableLayout

android:stretchColumns

The zero-based index of the columns to stretch. The column indices must be separated by a comma: 1, 2, 5. Illegal and duplicate indices are ignored. You can stretch all columns by using the value "*" instead. Note that a column can be marked stretchable and shrinkable at the same time.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:stretchColumns="*"
    android:descendantFocusability="blocksDescendants">

.....

</TableLayout>

Hope this will help.

I think what you want is tableLayout.setStretchAllColumns(true);. Unfortunately you cannot set that in xml, perhaps place it in your adapter.

None of the mentioned solutions have helped me, so trying to solve this for 3 days I found a solution myself. Unfortunately, the solution is programmatical, the following code is to be added to your custom adapter:

trackCheckBox.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
trackNumberText.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int width = context.getResources().getDisplayMetrics().widthPixels - trackCheckBox.getMeasuredWidth() - trackNumberText.getMeasuredWidth();

trackArtistText.setWidth(width);
trackTitleText.setWidth(width);
Related