TabHost in Android with Example
Last Updated :
25 Jul, 2024
TabHost is a container that holds a set of tabs. Each tab consists of either the activity or the fragments. TabHost consists of two children of which one is FrameLayout (which is used to show the contents of the activity) and another one is TabWidget.(It is used to choose the tab which the user wants to open). Mostly Tabhost is used in the project with the help of the LinearLayout.
Important Methods of TabHost
addTab(TabSpec tabSpec): This method is used to add the new tab onto a tab widget. Whenever a new tab is being specified using TabSpec class, one needs to add that tab in our TabHost.
// initiate TabHost
TabHost tabhost = findViewById(R.id.tabhost);
// setting up the tabhost
tabhost.setup();
// setting the name of the new tab
TabHost.TabSpec spec = tabhost.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
spec.setIndicator("Tab One");
// adding the tab to tabhost
tabhost.addTab(spec);
clearAllTabs(): This method is used to clear all tabs within the tab host. After adding the tab as shown above, if some want to clear the tab from TabHost, then write the below code.
tabHost.clearAllTabs(); // this method is used to clear all the tabs from tabhost
setOnTabChangedListener(OnTabChangeListener): This method is used to register a callback that needs to be invoked when the selected state of any of the items in this list changes. This method is used when one needs to invoke the callback and register it when the state changes of any selected items in the list.
setCurrentTab(int index): By default, tab hosts set the first tab position as the default position which will appear when the app is being launched, but we can explicitly change the default position of the tab using these methods. (Position starts from 0)
tabHost.setCurrentTab(1); // it will set second tab as default selected tab
Example
Let’s try to understand TabHost in detail by making a small project. A sample GIF is given below to get an idea about what we are going to do in this project. We are going to implement this project using both Java and Kotlin Programming languages for Android.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<?xml version="1.0" encoding="utf-8"?><!--Linear layout as the root layout-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Tab widget to select the tab -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- FrameLayout which contains the data of the activity -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- for tab 1 -->
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFC0CB"
android:orientation="vertical">
<!-- Text View for applying the text to the tab -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="270dp"
android:text="This is Tab 1"
android:textColor="#000"
android:textSize="32sp" />
</LinearLayout>
<!-- for tab 2 -->
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90ee90"
android:orientation="vertical">
<!-- Text View for applying the text to the tab -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="270dp"
android:text="This is Tab 2"
android:textColor="#000"
android:textSize="32sp" />
</LinearLayout>
<!-- for tab 3 -->
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#add8e6"
android:orientation="vertical">
<!-- Text View for applying the text to the tab -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="270dp"
android:text="This is Tab 3"
android:textColor="#000"
android:textSize="32sp" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Step 3: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.widget.TabHost;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiating the tabhost
TabHost tabhost = findViewById(R.id.tabhost);
// setting up the tab host
tabhost.setup();
// Code for adding Tab 1 to the tabhost
TabHost.TabSpec spec = tabhost.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
// setting the name of the tab 1 as "Tab One"
spec.setIndicator("Tab One");
// adding the tab to tabhost
tabhost.addTab(spec);
// Code for adding Tab 2 to the tabhost
spec = tabhost.newTabSpec("Tab Two");
spec.setContent(R.id.tab2);
// setting the name of the tab 1 as "Tab Two"
spec.setIndicator("Tab Two");
tabhost.addTab(spec);
// Code for adding Tab 3 to the tabhost
spec = tabhost.newTabSpec("Tab Three");
spec.setContent(R.id.tab3);
spec.setIndicator("Tab Three");
tabhost.addTab(spec);
}
}
Kotlin
import android.os.Bundle
import android.widget.TabHost
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initiating the tabhost
val tabhost = findViewById<TabHost>(R.id.tabhost)
// setting up the tab host
tabhost.setup()
// Code for adding Tab 1 to the tabhost
var spec = tabhost.newTabSpec("Tab One")
spec.setContent(R.id.tab1)
// setting the name of the tab 1 as "Tab One"
spec.setIndicator("Tab One")
// adding the tab to tabhost
tabhost.addTab(spec)
// Code for adding Tab 2 to the tabhost
spec = tabhost.newTabSpec("Tab Two")
spec.setContent(R.id.tab2)
// setting the name of the tab 1 as "Tab Two"
spec.setIndicator("Tab Two")
tabhost.addTab(spec)
// Code for adding Tab 3 to the tabhost
spec = tabhost.newTabSpec("Tab Three")
spec.setContent(R.id.tab3)
spec.setIndicator("Tab Three")
tabhost.addTab(spec)
}
}
Output:
Similar Reads
TabHost in Android with Example
TabHost is a container that holds a set of tabs. Each tab consists of either the activity or the fragments. TabHost consists of two children of which one is FrameLayout (which is used to show the contents of the activity) and another one is TabWidget.(It is used to choose the tab which the user want
5 min read
PhotoView in Android with Example
In this article, PhotoView is added to android. PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView using multi-touch and double-tap. Besides that, it has many more features like it notifying the application when the user taps on the photo or when the displa
2 min read
TextView in Android with Example
TextView is a simple widget that is seen in every android application. This widget is used to display simple text within the android application. We can add custom styling to the text that we have to show. In this article, we will take a look at How to create a simple Text View in an android applica
2 min read
Notifications in Android with Example
Notification is a kind of message, alert, or status of an application (probably running in the background) that is visible or available in the Android's UI elements. This application could be running in the background but not in use by the user. The purpose of a notification is to notify the user ab
7 min read
Spinner in Android with Example
Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
4 min read
Android Sensors with Example
In our childhood, we all have played many android games like Moto Racing and Temple run in which by tilting the phone the position of the character changes. So, all these happen because of the sensors present in your Android device. Most Android-powered devices have built-in sensors that measure mot
4 min read
TextWriter in Android with Example
TextWriter is used to animate text. TextWriter can be used when users open the app i.e. in place of Splash Screen. One can also use Splash Screen instead of TextWriter but TextWriter is an animation library and it is known that animations help to gain the attention of the user so it is best to learn
2 min read
Android ListView in Java with Example
A ListView in Android is a type of AdapterView that displays a vertically scrollable list of items, with each item positioned one below the other. Using an adapter, items are inserted into the list from an array or database efficiently. For displaying the items in the list method setAdaptor() is use
3 min read
OpenIntents in Android with Example
OI refers to the "OpenIntents" project in Android are a way for one app to request an action from another app. This can be done using either explicit or implicit intents, allowing them to share functionality. The OpenIntents project provides a set of commonly-used intents that can be used by develop
4 min read
TreeView in Android with Example
If you are looking for new UI designs to represent huge data, then there are so many ways to represent this type of data. You can use pie charts, graphs, and many more view types to implement these views. For displaying such huge data then we can prefer using a TreeView. TreeView is similar to that
5 min read