Android TableLayout Tutorial with Example kotlin

Android TableLayout is provided row and column to manage UI data. You can manage your data in tabular format. Like you have a list of student in SQLite database and you want to show this data. Important point is that TableLayout doesn’t display border lines on rows and Each row can have zero or more cells.

Android TableLayout Tutorial with Example kotlin

Attributes of Android TableLayout :

Here is some important attribute of TableLayout in Android.

android:id

This is the ID which uniquely identifies the layout.

android:stretchColumns

Stretchable, it can expand in width to fit any extra space. The zero-based index of the columns to stretch. And column indices must be separated by a comma: 1, 2, 3.

android:shrinkColumns

Shrinkable, the column width can be shrunk to fit the table into its parent object. The zero-based index of the columns to shrink. The column indices must be separated by a comma: 1, 2, 4.

android:collapseColumns

You can hide a column by calling. This specifies the zero-based index of the columns to collapse. For multiple columns do separate by a comma: 1, 3, 5.

Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

 

Complete Example of Android TableLayout :

Here we are using TableLayout as a base layout and TableRow to vertical data show. In <TableRow> we are using <TextView>. Check this example for good understanding.

 

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Open  “colors.xml” resource file and add these colors

Step 3. Add <TableLayout> and others following code in “activity_main.xml” resource file

Step 4. Open and setContentView res layout file in “MainActivity.kt

Step 5. Run the application, in an emulator or On your android device
Output screenshot Android Table Layout example :

 

Android TableLayout Tutorial with Example kotlin output

 

Download source code Android TableLayout in kotlin

https://github.com/EyeHunts/AndroidTableLayout

 

Do comment if you have any doubt and suggestion on this tutorial

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.