Last Updated:

TableLayout container in Android [Case study]

The TableLayout container in Android is designed to organize an internal hierarchy of widgets in the form of rows and columns. The element of this container is used to build a row in the table. Each row has 0 or more cells, and each cell can contain a single View object.

TableLayout container attributes


  • android:id is a unique identifier that you can use to work with the container.
  • Attribute android:stretchColumns. With it, we can stretch the width of the column. Column indexes start at zero and must be separated by a comma, for example: 1, 2, 5. We can also specify an asterisk '*' as a value to stretch all the columns. Example: we want to insert one widget in three rows of the TableLayout container and make them occupy the entire width of their row. To do this, we just need to add the android:stretchColumns attribute with the value "*".
  • android:collapseColumns. With it, we can make a column invisible by specifying one or more comma-separated columns. Column indexes start at zero and must be separated by a comma, for example: 1, 2, 5. These columns are part of the table, but are not visible.
  • The android:layout_column attribute is used to specify the column in which the View object should be placed. By default, any View is placed in the first unused column.
  • The android:layout_span attribute is used when the View object must span more than one column. For example, if we have a row with three elements and each element has android:layout_span="2", then you will have at least six columns in the table.

Example of using TableLayout in Android


First, let's create a new Android project. You can also use your own, already existing project: you will just need to add the files below to it:

Now let's create a layout:

On preview, it will look like this:


Now in the TableActivity class, let's try to programmatically create everything that we have just described in the layout:

Now let's run our software-generated TableLayout container on the device and see the following.

TableLayout Container

This was a quick article to familiarize yourself with the TableLayout container and its attributes. Follow the updates of the section "Development for Android".