Android TableLayout in Kotlin - GeeksforGeeks

21 Feb, 2022
Android TableLayout is a ViewGroup subclass which is used to display the child View elements in rows and columns. It will arrange all the children elements into rows and columns and does not display any border lines in between rows, columns or cells.
The working of TableLayout is almost similar to HTML table and it contains as many columns as row with the most cells.
The TableLayout can be defined using <TableLayout> like below:
XML
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginTop="10dp"    android:paddingLeft="5dp"    android:paddingRight="5dp">       // Add Table rows here</TableLayout>
and TableRow can be defined using
XML
<TableRow android:background="#51B435" android:padding="10dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Rank" /></TableRow>
In this file, we declare the TableLayout and start adding table rows with the help of TableRow. We are creating ranking table of players where we define four columns Rank, Name, Country and Points.
The code for the Table is:
XML
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginTop="10dp"    android:paddingLeft="5dp"    android:paddingRight="5dp">      <TextView        android:id="@+id/txt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="ICC Ranking of Players:"        android:textSize = "20dp"        android:textStyle="bold">      </TextView>        <TableRow android:background="#51B435" android:padding="10dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Rank" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Player" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Team" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Points" />    </TableRow>    <TableRow android:background="#F0F7F7" android:padding="5dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="1" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Virat Kohli" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="IND" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="895" />    </TableRow>    <TableRow android:background="#F0F7F7" android:padding="5dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="2" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Rohit Sharma" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="IND" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="863" />      </TableRow>    <TableRow android:background="#F0F7F7" android:padding="5dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="3" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Faf du Plessis" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="PAK" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="834" />      </TableRow>      <TableRow android:background="#F0F7F7" android:padding="5dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="4" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Steven Smith" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="AUS" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="820" />      </TableRow>      <TableRow android:background="#F0F7F7" android:padding="5dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="5" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Ross Taylor" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="NZ" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="817" />      </TableRow>  </TableLayout>
When we have created layout, we need to load the XML layout resource from our activity onCreate() callback method and access the UI element form the XML using findViewById.
Kotlin
package com.geeksforgeeks.myfirstKotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundle    class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        // finding the UI elements        }}
We need to run using Android Virtual Device(AVD) to see the output.
ayushpandey3july
Kotlin Android
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
Content Providers in Android with Example
Retrofit with Kotlin Coroutine in Android
Kotlin Setters and Getters
How to Add and Customize Back Button of Action Bar in Android?
How to Change the Color of Status Bar in an Android App?
Kotlin Higher-Order Functions
How to Get Current Location in Android?