8

I can set android:shrinkColumns and android:stretchColumns at TableLayout.

For example:

<TableLayout
    android:shrinkColumns="2,3"
    android:stretchColumns="1,3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

So how do this properties affects columns?

Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
  • 1
    May I suggest reading the [Android Developer Guide](https://developer.android.com/guide/index.html) and [Android Docs](http://developer.android.com/reference/packages.html) - The docs for TableLayout clearly explain what these do – Ed Holloway-George Sep 01 '15 at 12:19

2 Answers2

15
  • 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.

  • android:shrinkColumns

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

  • android:collapseColumns

    The zero-based index of the columns to collapse. The column indices must be separated by a comma: 1, 2, 5. Illegal and duplicate indices are ignored.

    <TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:background="@color/grey">
    
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:background="@color/red"
            android:textColor="@android:color/white"
            android:textSize="30dp"
            android:text="1" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:textColor="@android:color/white"
            android:textSize="30dp"
            android:background="@color/green"
            android:text="2" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:textColor="@android:color/white"
            android:textSize="30dp"
            android:background="@color/blue"
            android:text="3" />
    </TableRow>
    
    
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:background="@color/red"
            android:textColor="@android:color/white"
            android:textSize="30dp"
            android:text="1" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:textColor="@android:color/white"
            android:textSize="30dp"
            android:background="@color/green"
            android:text="2" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:textColor="@android:color/white"
            android:textSize="30dp"
            android:background="@color/blue"
            android:text="3" />
    </TableRow>
    </TableLayout>
    

Explanations:

android:stretchColumns="*"

enter image description here

Means it stretch all columns equally according to table layout width

android:shrinkColumns="*"

enter image description here

Means it shrink all columns

android:shrinkColumns="0,2"

android:stretchColumns="1"

enter image description here

Means column 0 and 2 are wraps contain and column 1 stretch into the available width

android:stretchColumns="0,1,2"

android:shrinkColumns="1"

enter image description here

Means if column already stretch then shrink not apply

android:shrinkColumns="*"

android:collapseColumns="1"

enter image description here

android:collapseColumns means it hides given column

android:stretchColumns="*"

TextView :- android:layout_column="2"

enter image description here

Meaning if table row first column layout parameter does not start with 0 then empty view added into the row

android:stretchColumns="*"

android:collapseColumns="1"

TextView :- android:layout_column="2"

enter image description here

Means if table-row first column layout parameter does not start with 0 then empty view added into row but if you collapse column then added empty view not hide of that column index only hide added view by explicit view

I hope this will help.

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
BiRjU
  • 733
  • 6
  • 23
  • stretchColumns and shrinkColumns are 0 based column. That means the first column is 0 not 1. – Simon Mar 07 '20 at 02:26
  • Thank you sir, very nice explanation – FireTr3e Nov 13 '21 at 18:07
  • Why do you need android:layout_column="0", etc. as it is auto assigned – Payel Senapati Apr 10 '22 at 09:11
  • @PayelSenapati good question..i write every child with its position because while reading or coding its easy to understnd which coloumn i am working on…what have to do when i have more then 15-20 or 50 coloumn how to know while scrolling up/down in xml which coloumn i am currenty focusing ? its easy to describe every coloumn with its position so don’t have to confuse or count every coloumn while scrolling or doing some operation …i just read the coloumn index from child view its easy for me – BiRjU Apr 14 '22 at 06:33
7

TableLayout can specify certain columns as shrinkable or stretchable by calling setColumnShrinkable()(xml:android:shrinkColumns) or setColumnStretchable()(xml:android:stretchColumns).

If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space.

The total width of the table is defined by its parent container. It is important to remember that a column can be both shrinkable and stretchable .

For details information you may visit

https://developer.android.com/reference/android/widget/TableLayout.html

Chin
  • 19,717
  • 37
  • 107
  • 164
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198