How to use TabHost in Kotlin

In this android kotlin source code example, we are going to use TabHost in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to use TabHost in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Tab.TabActivity17">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>
    </TabHost>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import android.app.TabActivity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TabHost
import com.bluapp.kotlinview.R


@Suppress("DEPRECATION")
class TabActivity17 : TabActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tab17)
        val tabHost = findViewById(android.R.id.tabhost) as TabHost

        val TabMenu1: TabHost.TabSpec = tabHost.newTabSpec("First Tab")
        val TabMenu2: TabHost.TabSpec = tabHost.newTabSpec("Second Tab")
        val TabMenu3: TabHost.TabSpec = tabHost.newTabSpec("Third Tab")

        TabMenu1.setIndicator("Tab1")
        TabMenu1.setContent(Intent(this@TabActivity17, TabHostActivity::class.java))

        TabMenu2.setIndicator("Tab2")
        TabMenu2.setContent(Intent(this@TabActivity17, TabHostActivity::class.java))

        TabMenu3.setIndicator("Tab3")
        TabMenu3.setContent(Intent(this@TabActivity17, TabHostActivity::class.java))

        tabHost.addTab(TabMenu1)
        tabHost.addTab(TabMenu2)
        tabHost.addTab(TabMenu3)

    }
}

activity_tab_host.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Tab.TabHostActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="TabHost and TabWidget"
        android:gravity="center"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

TabHostActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.bluapp.kotlinview.R

class TabHostActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tab_host)
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment