Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.Hello Readers! In this post, we are going to learn about how to use android tabHost in kotlin in any android application. We will also go through different attributes of TabHost widget that can be used to customise it.
Output

Tutorialwing Android TabHost Output
Getting Started
TabHost widget can be defined as below –
TabHost is a container for the tabbed window view.
TabHost contains two children. They are –
(a) A set of tab labels that user clicks to select a specific tab.
(b) A FrameLayout object that displays the selected tab views.
Attributes of Android TabHost Widget
Some of the popular attributes of android TabHost widget inherited from FrameLayout are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:foregroundGravity | Defines the gravity of the foreground drawable |
2 | android:measureAllChildren | Defines whether to measure all children or only those in VISIBLE or INVISIBLE state when measuring |
Some of the popular attributes of android TabHost widget inherited from ViewGroup are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animateLayoutChanges | Defines whether LayoutTransition should run whenever there is any changes in layout |
2 | android:animationCache | Defines whether layout animations should create a drawing cache for their children. |
3 | android:clipToPadding | Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero. |
4 | android:layoutAnimation | Defines the layout animation to use the first time the ViewGroup is laid out. |
5 | android:layoutMode | Defines the layout mode of this viewGroup |
Some of the popular attributes of android TabHost widget inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:alpha | Defines the alpha of the view |
2 | android:background | Defines the background of the view |
3 | android:padding | Defines padding of the view for all edges |
4 | android:tooltipText | Defines text displayed in a small popup window on hover or long press |
5 | android:clickable | Defines whether view is clickable or not |
6 | android:theme | Defines a theme override for view |
7 | android:id | Defines id of the view |
8 | android:padding | Defines padding of the view |
Example of Android TabHost In Kotlin
At first, we will create android application. Then, we will use TabHost widget in this application.
1. Creating New Project in Kotlin
Follow steps below to create new project. Please ignore the steps if you have already created the project.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as TabHost. Then, check Include Kotlin Support and click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | You will get a newly created project successfully if you have followed steps properly. |
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">TabHost</string> <string name="presented_by">Presented by Tutorialwing.com</string> <string name="no_image">No Image</string> <string name="profile">Profile</string> <string name="recent">Recent</string> <string name="home">Home</string> </resources>
3. Create Pages for Each Tab in TabHost
In this tutorial, we are going to show three tabs in the application. So, we will create three pages for tab. Their name would be as follows –
(a) HomeActivity.kt and xml file (activity_main.xml)
(b) RecentActivity.kt and xml file (activity_recent.xml)
(c) ProfileActivity.kt and xml file (activity_profile.xml)
3.1 Create First Page(Home Page) of TabHost
Create a new kotlin file, HomeActivity.kt, in main/java/com.tutorialwing.tabhost folder. Then, add below code into it.
package com.tutorialwing.tabhost import android.os.Bundle import android.support.v7.app.AppCompatActivity class HomeActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_home) } }
Now, create xml file for this activity class in res/layout folder and name it activity_home.xml. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_dark" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:text="@string/presented_by" android:textColor="@android:color/white" android:textSize="18sp" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:contentDescription="@string/no_image" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/home" android:textColor="@android:color/white" android:textSize="17sp"/> </LinearLayout>
3.2 Create Second Page(Recent Page) Of TabHost
Create a new kotlin file, RecentActivity.kt, in main/java/com.tutorialwing.tabhost folder. Then, add below code into it.
package com.tutorialwing.tabhost import android.os.Bundle import android.support.v7.app.AppCompatActivity class RecentActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_recent) } }
Now, create xml file for this activity class in res/layout folder and name it activity_recent.xml. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_red_dark" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:text="@string/presented_by" android:textColor="@android:color/white" android:textSize="18sp" android:textStyle="bold"/> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:contentDescription="@string/no_image" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/txtMain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/recent" android:textColor="@android:color/white" android:textSize="17sp"/> </LinearLayout>
3.3 Create Third Page(Profile Page) Of TabHost
Create a new kotlin file, ProfileActivity.kt, in main/java/com.tutorialwing.tabhost folder. Then, add below code into it.
package com.tutorialwing.tabhost import android.os.Bundle import android.support.v7.app.AppCompatActivity class ProfileActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_profile) } }
Now, create xml file for this activity class in res/layout folder and name it activity_profile.xml. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_green_dark" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:text="@string/presented_by" android:textColor="@android:color/white" android:textSize="18sp" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:contentDescription="@string/no_image" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/profile" android:textColor="@android:color/white" android:textSize="17sp"/> </LinearLayout>
4. Use TabHost Widget in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <TabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@android:id/tabs"> </FrameLayout> </RelativeLayout> </TabHost>
In activity_main.xml file, we have defined tabHost. Inside tabHost, we have tabWidget, that shows tab labels, and frameLayout that shows views for selected tab. In our case, we will show a separate activity for each page whenever any tab is selected. Now, we will access this tabHost in kotlin file and perform some operations on it.
5. Access TabHost in Kotlin file
Open src/main/java/com.tutorialwing.tabhost/MainActivity.kt file. Then, add below code into it.
package com.tutorialwing.tabhost import android.app.TabActivity import android.content.Intent import android.os.Bundle import android.widget.TabHost import android.widget.Toast class MainActivity : TabActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val tabHost = findViewById<TabHost>(android.R.id.tabhost) if (tabHost != null) { addTab(tabHost, getString(R.string.home), getString(R.string.home), HomeActivity::class.java) addTab(tabHost, getString(R.string.recent), getString(R.string.recent), RecentActivity::class.java) addTab(tabHost, getString(R.string.profile), getString(R.string.profile), ProfileActivity::class.java) tabHost.currentTab = 1 tabHost.setOnTabChangedListener { tabId -> Toast.makeText(applicationContext, tabId, Toast.LENGTH_SHORT).show() } } } private fun addTab(tabHost: TabHost, name: String, indicator: String, className: Class<*>) { val tabSpec = tabHost.newTabSpec(name) tabSpec.setIndicator(indicator) val intent = Intent(this, className) tabSpec.setContent(intent) tabHost.addTab(tabSpec) } }
We have accessed tabHost in kotlin file ( i.e. in MainActivity.kt file). Then, we have added three tab to tabHost. After that, we have defined tab change listener that displays currently selected tab.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.tabhost" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".RecentActivity" android:label="@string/recent"> </activity> <activity android:name=".HomeActivity" android:label="@string/home"> </activity> <activity android:name=".ProfileActivity" android:label="@string/profile"> </activity> </application> </manifest>
When we run the program, we will get output as shown above.
That’s end of tutorial on Android TabHost in Kotlin.