to top
Android APIs
public class

TableLayout

extends LinearLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.widget.TableLayout

Class Overview

A layout that arranges its children into rows and columns. A TableLayout consists of a number of 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 WRAP_CONTENT. If the child is a TableRow, then the height is always 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
class TableLayout.LayoutParams

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. 

XML Attributes
Attribute Name Related Method Description
android:collapseColumns setColumnCollapsed(int,boolean) The zero-based index of the columns to collapse. 
android:shrinkColumns setShrinkAllColumns(boolean) The zero-based index of the columns to shrink. 
android:stretchColumns setStretchAllColumns(boolean) The zero-based index of the columns to stretch. 
[Expand]
Inherited XML Attributes
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
TableLayout(Context context)

Creates a new TableLayout for the given context.

TableLayout(Context context, AttributeSet attrs)

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

Public Methods
void addView(View child)

Adds a child view.

void addView(View child, int index)
Adds a child view.
void addView(View child, int index, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
void addView(View child, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
TableLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
boolean isColumnCollapsed(int columnIndex)

Returns the collapsed state of the specified column.

boolean isColumnShrinkable(int columnIndex)

Returns whether the specified column is shrinkable or not.

boolean isColumnStretchable(int columnIndex)

Returns whether the specified column is stretchable or not.

boolean isShrinkAllColumns()

Indicates whether all columns are shrinkable or not.

boolean isStretchAllColumns()

Indicates whether all columns are stretchable or not.

void onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
void requestLayout()
Call this when something has changed which has invalidated the layout of this view.
void setColumnCollapsed(int columnIndex, boolean isCollapsed)

Collapses or restores a given column.

void setColumnShrinkable(int columnIndex, boolean isShrinkable)

Makes the given column shrinkable or not.

void setColumnStretchable(int columnIndex, boolean isStretchable)

Makes the given column stretchable or not.

void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener)
Register a callback to be invoked when a child is added to or removed from this view.
void setShrinkAllColumns(boolean shrinkAllColumns)

Convenience method to mark all columns as shrinkable.

void setStretchAllColumns(boolean stretchAllColumns)

Convenience method to mark all columns as stretchable.

Protected Methods
boolean checkLayoutParams(ViewGroup.LayoutParams p)
LinearLayout.LayoutParams generateDefaultLayoutParams()
Returns a set of layout parameters with a width of MATCH_PARENT, and a height of WRAP_CONTENT.
LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
Returns a safe set of layout parameters based on the supplied layout params.
void onLayout(boolean changed, int l, int t, int r, int b)
Called from layout when this view should assign a size and position to each of its children.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

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

[Expand]
Inherited Methods
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

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.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol collapseColumns.

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.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol shrinkColumns.

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.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol stretchColumns.

Public Constructors

public TableLayout (Context context)

Added in API level 1

Creates a new TableLayout for the given context.

Parameters
context the application environment

public TableLayout (Context context, AttributeSet attrs)

Added in API level 1

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

Parameters
context the application environment
attrs a collection of attributes

Public Methods

public void addView (View child)

Added in API level 1

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 the child view to add

public void addView (View child, int index)

Added in API level 1

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 the child view to add
index the position at which to add the child

public void addView (View child, int index, ViewGroup.LayoutParams params)

Added in API level 1

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 the child view to add
index the position at which to add the child
params the layout parameters to set on the child

public void addView (View child, ViewGroup.LayoutParams params)

Added in API level 1

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 the child view to add
params the layout parameters to set on the child

public TableLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Added in API level 1

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

Parameters
attrs the attributes to build the layout parameters from
Returns

public boolean isColumnCollapsed (int columnIndex)

Added in API level 1

Returns the collapsed state of the specified column.

Parameters
columnIndex the index of the column
Returns
  • true if the column is collapsed, false otherwise

public boolean isColumnShrinkable (int columnIndex)

Added in API level 1

Returns whether the specified column is shrinkable or not.

Parameters
columnIndex the index of the column
Returns
  • true if the column is shrinkable, false otherwise. Default is false.

public boolean isColumnStretchable (int columnIndex)

Added in API level 1

Returns whether the specified column is stretchable or not.

Parameters
columnIndex the index of the column
Returns
  • true if the column is stretchable, false otherwise

public boolean isShrinkAllColumns ()

Added in API level 1

Indicates whether all columns are shrinkable or not.

Related XML Attributes
Returns
  • true if all columns are shrinkable, false otherwise

public boolean isStretchAllColumns ()

Added in API level 1

Indicates whether all columns are stretchable or not.

Related XML Attributes
Returns
  • true if all columns are stretchable, false otherwise

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public void requestLayout ()

Added in API level 1

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.

public void setColumnCollapsed (int columnIndex, boolean isCollapsed)

Added in API level 1

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 TableRow.

Calling this method requests a layout operation.

Related XML Attributes
Parameters
columnIndex the index of the column
isCollapsed true if the column must be collapsed, false otherwise

public void setColumnShrinkable (int columnIndex, boolean isShrinkable)

Added in API level 1

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.

Related XML Attributes
Parameters
columnIndex the index of the column
isShrinkable true if the column must be shrinkable, false otherwise. Default is false.

public void setColumnStretchable (int columnIndex, boolean isStretchable)

Added in API level 1

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.

Related XML Attributes
Parameters
columnIndex the index of the column
isStretchable true if the column must be stretchable, false otherwise. Default is false.

public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

Added in API level 1

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

Parameters
listener the callback to invoke on hierarchy change

public void setShrinkAllColumns (boolean shrinkAllColumns)

Added in API level 1

Convenience method to mark all columns as shrinkable.

Related XML Attributes
Parameters
shrinkAllColumns true to mark all columns shrinkable

public void setStretchAllColumns (boolean stretchAllColumns)

Added in API level 1

Convenience method to mark all columns as stretchable.

Related XML Attributes
Parameters
stretchAllColumns true to mark all columns stretchable

Protected Methods

protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

Added in API level 1

protected LinearLayout.LayoutParams generateDefaultLayoutParams ()

Added in API level 1

Returns a set of layout parameters with a width of MATCH_PARENT, and a height of WRAP_CONTENT.

Returns
  • a set of default layout parameters or null

protected LinearLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Added in API level 1

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 The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Returns

protected void onLayout (boolean changed, int l, int t, int r, int b)

Added in API level 1

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 This is a new size or position for this view
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Added in API level 1

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 overriden 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 horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.