In android TableLayout shrinkColumns appears to be the default attribute even if not explicitly stated

33 Views Asked by At

In the following code, there are two tableLayouts with 3 columns each -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:stretchColumns="0,1">

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test1"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test2"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test3"/>

        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:stretchColumns="0,1"
        android:shrinkColumns="2">

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test1"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test2"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test3"/>

        </TableRow>

    </TableLayout>

</LinearLayout>

The first TableLayout has no mention of attribute for column2 and the second table layout has an explicit mention of attribute for column2 to be columnShrink. And both apparently give same output.

So, is it safe to assume that shrinkColumns is the default attribute even if not explicitly stated?

1

There are 1 best solutions below

0
Andrew On

With that amount of text at the default font size I would expect the same output.

As the TableLayout Docs say

The total width of the table is defined by its parent container.

So if you don't specify and grow/shrinkable it will grow/shrink them all equally to match the parent.

But in you case TableLayout will because you have only one row, it first sizes them to basically wrap_content and finds that this does not match the parent width and therefore it will grow column 0 and 1 equally to fill the space.

Because it is not short of space it (because it can grow) then it will never engage any shrink functionality because from the docs

If marked as stretchable, it can expand in width to fit any extra space

The key word here is extra

You would only expect it shrink a column if the text sized at wrap_content is wider than the screen (and therefore no column will be grown)

So really android:shrinkColumns="2" on table is redundant and has no affect, the default of wrap_content will be used

Basically growing will never force any other column to shrink.