Table Layout Code Snippet for Android Studio


Welcome. In this tutorial you will learn about Table layout in Android Studio.

Android Table Layout Example

A table layout is a type of layout which is divided into rows and columns. The UI elements are arranged accordingly in rows or columns. The TableRow property is used to define a row and you can insert as many cells inside a row as you want. Each cell can hold one or more UI element in it. Cells can even be empty.

The table will have as many columns as the row which has most number of cells. However the border of the cells will not be shown. The width  of a cell depends on the widest cell in that row. You can also make certain columns shrinkable or stretchable, after which their width can be changed according to the space available. There are also other types of layouts available such as Linear Layout or Relative Layout.

Inserting a Table Layout in your Android Studio app

For inserting a Table layout in your app the <TableLayout> tag is used, as shown in the example below:-

Here the TableLayout is used to create the layout and we can see that the TableRow is used to create a row. For creating cells, you will have to insert your UI elements inside the Table Row. As many elements you will insert, that many cells will be created.

Note:- You cannot predefine the width of cells in table layout. The cells will arrange themselves according to the parent width. However you can specify the height of the cells.


An example of Tale Layout is shown in the Image below:-

Table Layout Example Code For Android Studio


The Full Java Code along with the XML layout for creating the above UI is shown below:-



XML (activity_main.xml)



You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with various other layout settings on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your UI more interesting.

For a more detailed guide, you can also visit android’s official guide below:-

Comment down below if you are facing any problems.


Also Read:-

Leave a Reply

Your email address will not be published. Required fields are marked *