Added in API level 1

TableLayout


open class TableLayout : LinearLayout
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.LinearLayout
   ↳ android.widget.TableLayout

A layout that arranges its children into rows and columns. A TableLayout consists of a number of android.widget.TableRow objects, each defining a row (actually, you can have other children, which will be explained below). TableLayout containers do not display border lines for their rows, columns, or cells. Each row has zero or more cells; each cell can hold one View object. The table has as many columns as the row with the most cells. A table can leave cells empty. Cells can span columns, as they can in HTML.

The width of a column is defined by the row with the widest cell in that column. However, a TableLayout can specify certain columns as shrinkable or stretchable by calling setColumnShrinkable() or setColumnStretchable(). 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. In such a situation, the column will change its size to always use up the available space, but never more. Finally, you can hide a column by calling setColumnCollapsed().

The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT. However, the layout_height attribute can be defined by a child; default value is android.widget.TableLayout.LayoutParams#WRAP_CONTENT. If the child is a android.widget.TableRow, then the height is always android.widget.TableLayout.LayoutParams#WRAP_CONTENT.

Cells must be added to a row in increasing column order, both in code and XML. Column numbers are zero-based. If you don't specify a column number for a child cell, it will autoincrement to the next available column. If you skip a column number, it will be considered an empty cell in that row. See the TableLayout examples in ApiDemos for examples of creating tables in XML.

Although the typical child of a TableLayout is a TableRow, you can actually use any View subclass as a direct child of TableLayout. The View will be displayed as a single row that spans all the table columns.

Summary

Nested classes
open

This set of layout parameters enforces the width of each child to be MATCH_PARENT and the height of each child to be WRAP_CONTENT, but only if the height is not specified.

Inherited XML attributes
Inherited constants
Public constructors
TableLayout(context: Context!)

Creates a new TableLayout for the given context.

TableLayout(context: Context!, attrs: AttributeSet!)

Creates a new TableLayout for the given context and with the specified set attributes.

Public methods
open Unit
addView(child: View!)

Adds a child view.

open Unit
addView(child: View!, index: Int)

Adds a child view.

open Unit
addView(child: View!, params: ViewGroup.LayoutParams!)

Adds a child view with the specified layout parameters.

open Unit
addView(child: View!, index: Int, params: ViewGroup.LayoutParams!)

Adds a child view with the specified layout parameters.

open TableLayout.LayoutParams!

Returns a new set of layout parameters based on the supplied attributes set.

open CharSequence!

open Boolean
isColumnCollapsed(columnIndex: Int)

Returns the collapsed state of the specified column.

open Boolean
isColumnShrinkable(columnIndex: Int)

Returns whether the specified column is shrinkable or not.

open Boolean
isColumnStretchable(columnIndex: Int)

Returns whether the specified column is stretchable or not.

open Boolean

Indicates whether all columns are shrinkable or not.

open Boolean

Indicates whether all columns are stretchable or not.

open Unit

Call this when something has changed which has invalidated the layout of this view.

open Unit
setColumnCollapsed(columnIndex: Int, isCollapsed: Boolean)

Collapses or restores a given column.

open Unit
setColumnShrinkable(columnIndex: Int, isShrinkable: Boolean)

Makes the given column shrinkable or not.

open Unit
setColumnStretchable(columnIndex: Int, isStretchable: Boolean)

Makes the given column stretchable or not.

open Unit

Register a callback to be invoked when a child is added to or removed from this view.

open Unit
setShrinkAllColumns(shrinkAllColumns: Boolean)

Convenience method to mark all columns as shrinkable.

open Unit
setStretchAllColumns(stretchAllColumns: Boolean)

Convenience method to mark all columns as stretchable.

Protected methods
open Boolean

open LinearLayout.LayoutParams!

Returns a set of layout parameters with a width of android.view.ViewGroup.LayoutParams#MATCH_PARENT, and a height of android.view.ViewGroup.LayoutParams#WRAP_CONTENT.

open LinearLayout.LayoutParams!

Returns a safe set of layout parameters based on the supplied layout params.

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

Called from layout when this view should assign a size and position to each of its children.

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Measure the view and its content to determine the measured width and the measured height.

Inherited functions
Inherited properties

Public constructors

TableLayout

Added in API level 1
TableLayout(context: Context!)

Creates a new TableLayout for the given context.

Parameters
context Context!: the application environment

TableLayout

Added in API level 1
TableLayout(
    context: Context!,
    attrs: AttributeSet!)

Creates a new TableLayout for the given context and with the specified set attributes.

Parameters
context Context!: the application environment
attrs AttributeSet!: a collection of attributes

Public methods

addView

Added in API level 1
open fun addView(child: View!): Unit

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child View!: the child view to add

addView

Added in API level 1
open fun addView(
    child: View!,
    index: Int
): Unit

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child View!: the child view to add
index Int: the position at which to add the child

addView

Added in API level 1
open fun addView(
    child: View!,
    params: ViewGroup.LayoutParams!
): Unit

Adds a child view with the specified layout parameters.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
view The view to be added to this window.
params ViewGroup.LayoutParams!: the layout parameters to set on the child
child View!: the child view to add

addView

Added in API level 1
open fun addView(
    child: View!,
    index: Int,
    params: ViewGroup.LayoutParams!
): Unit

Adds a child view with the specified layout parameters.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child View!: the child view to add
index Int: the position at which to add the child or -1 to add last
params ViewGroup.LayoutParams!: the layout parameters to set on the child

generateLayoutParams

Added in API level 1
open fun generateLayoutParams(attrs: AttributeSet!): TableLayout.LayoutParams!

Returns a new set of layout parameters based on the supplied attributes set.

Parameters
attrs AttributeSet!: the attributes to build the layout parameters from
Return
TableLayout.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

isColumnCollapsed

Added in API level 1
open fun isColumnCollapsed(columnIndex: Int): Boolean

Returns the collapsed state of the specified column.

Parameters
columnIndex Int: the index of the column
Return
Boolean true if the column is collapsed, false otherwise

isColumnShrinkable

Added in API level 1
open fun isColumnShrinkable(columnIndex: Int): Boolean

Returns whether the specified column is shrinkable or not.

Parameters
columnIndex Int: the index of the column
Return
Boolean true if the column is shrinkable, false otherwise. Default is false.

isColumnStretchable

Added in API level 1
open fun isColumnStretchable(columnIndex: Int): Boolean

Returns whether the specified column is stretchable or not.

Parameters
columnIndex Int: the index of the column
Return
Boolean true if the column is stretchable, false otherwise

isShrinkAllColumns

Added in API level 1
open fun isShrinkAllColumns(): Boolean

Indicates whether all columns are shrinkable or not.

Return
Boolean true if all columns are shrinkable, false otherwise

isStretchAllColumns

Added in API level 1
open fun isStretchAllColumns(): Boolean

Indicates whether all columns are stretchable or not.

Return
Boolean true if all columns are stretchable, false otherwise

requestLayout

Added in API level 1
open fun requestLayout(): Unit

Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.

Subclasses which override this method should call the superclass method to handle possible request-during-layout errors correctly.


If you override this method you must call through to the superclass implementation.

setColumnCollapsed

Added in API level 1
open fun setColumnCollapsed(
    columnIndex: Int,
    isCollapsed: Boolean
): Unit

Collapses or restores a given column. When collapsed, a column does not appear on screen and the extra space is reclaimed by the other columns. A column is collapsed/restored only when it belongs to a android.widget.TableRow.

Calling this method requests a layout operation.

Parameters
columnIndex Int: the index of the column
isCollapsed Boolean: true if the column must be collapsed, false otherwise

setColumnShrinkable

Added in API level 1
open fun setColumnShrinkable(
    columnIndex: Int,
    isShrinkable: Boolean
): Unit

Makes the given column shrinkable or not. When a row is too wide, the table can reclaim extra space from shrinkable columns.

Calling this method requests a layout operation.

Parameters
columnIndex Int: the index of the column
isShrinkable Boolean: true if the column must be shrinkable, false otherwise. Default is false.

setColumnStretchable

Added in API level 1
open fun setColumnStretchable(
    columnIndex: Int,
    isStretchable: Boolean
): Unit

Makes the given column stretchable or not. When stretchable, a column takes up as much as available space as possible in its row.

Calling this method requests a layout operation.

Parameters
columnIndex Int: the index of the column
isStretchable Boolean: true if the column must be stretchable, false otherwise. Default is false.

setOnHierarchyChangeListener

Added in API level 1
open fun setOnHierarchyChangeListener(listener: ViewGroup.OnHierarchyChangeListener!): Unit

Register a callback to be invoked when a child is added to or removed from this view.

Parameters
listener ViewGroup.OnHierarchyChangeListener!: the callback to invoke on hierarchy change

setShrinkAllColumns

Added in API level 1
open fun setShrinkAllColumns(shrinkAllColumns: Boolean): Unit

Convenience method to mark all columns as shrinkable.

Parameters
shrinkAllColumns Boolean: true to mark all columns shrinkable

setStretchAllColumns

Added in API level 1
open fun setStretchAllColumns(stretchAllColumns: Boolean): Unit

Convenience method to mark all columns as stretchable.

Parameters
stretchAllColumns Boolean: true to mark all columns stretchable

Protected methods

checkLayoutParams

Added in API level 1
protected open fun checkLayoutParams(p: ViewGroup.LayoutParams!): Boolean

generateDefaultLayoutParams

Added in API level 1
protected open fun generateDefaultLayoutParams(): LinearLayout.LayoutParams!

Returns a set of layout parameters with a width of android.view.ViewGroup.LayoutParams#MATCH_PARENT, and a height of android.view.ViewGroup.LayoutParams#WRAP_CONTENT.

Return
LinearLayout.LayoutParams! a set of default layout parameters or null

generateLayoutParams

Added in API level 1
protected open fun generateLayoutParams(p: ViewGroup.LayoutParams!): LinearLayout.LayoutParams!

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

Parameters
p ViewGroup.LayoutParams!: The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Return
LinearLayout.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

onLayout

Added in API level 1
protected open fun onLayout(
    changed: Boolean,
    l: Int,
    t: Int,
    r: Int,
    b: Int
): Unit

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed Boolean: This is a new size or position for this view
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent

onMeasure

Added in API level 1
protected open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int,int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int,int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int,int). Calling the superclass' onMeasure(int,int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int,int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.