Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The TableLayout
in Android is a layout where child views are organized in rows and columns. It's useful for creating grids or tabular layouts.
Here's a simple example of how you can use TableLayout
in an Android app with Kotlin:
<?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:padding="16dp" android:stretchColumns="1"> <!-- Row 1 --> <TableRow> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Key" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Value" /> </TableRow> <!-- Row 2 --> <TableRow> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Name" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="John Doe" /> </TableRow> <!-- Add more rows as needed --> </TableLayout>
Note that we're using android:stretchColumns="1"
which ensures the second column (0-indexed) stretches to fill any available space.
Here's a basic MainActivity
for the layout:
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
This is a static example, but you can also add rows dynamically in Kotlin by creating TableRow
instances programmatically, populating them with child views, and then adding them to the TableLayout
.
Remember to modify the AndroidManifest.xml
to specify the MainActivity
:
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
This is a basic example. Depending on your requirements, you might need to add more attributes or handle user interactions programmatically in Kotlin.
Creating a simple TableLayout in Android with Kotlin:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Rows and cells go here --> </TableLayout>
Android TableLayout rows and columns in Kotlin:
<TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Cells go here --> </TableRow>
Programmatically adding rows to TableLayout in Android Kotlin:
val tableLayout = findViewById<TableLayout>(R.id.tableLayout) val tableRow = TableRow(this) // Add cells to the TableRow // ... tableLayout.addView(tableRow)
Adding views to TableLayout dynamically in Kotlin:
val tableRow = TableRow(this) val textView = TextView(this) textView.text = "Cell Content" // Customize TextView attributes // ... tableRow.addView(textView)
TableLayout with TableRow and TextView in Android Kotlin:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:text="Cell 1" android:padding="8dp"/> <TextView android:text="Cell 2" android:padding="8dp"/> <!-- Add more cells as needed --> </TableRow> <!-- Add more rows as needed --> </TableLayout>