Hello Readers! In this post, we are going to learn about how to use android TabHost widget in any android application. We will also go through different attributes of TabHost widget that can be used to customise it.
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 | Specifies the gravity of the foreground drawable |
2 | android:measureAllChildren | Specifies 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 | Specifies whether LayoutTransition should run whenever there is any changes in layout |
2 | android:animationCache | Specifies whether layout animations should create a drawing cache for their children. |
3 | android:clipToPadding | Specifies 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 | Specifies the layout animation to use the first time the ViewGroup is laid out. |
5 | android:layoutMode | Specifies 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 | Specifies the alpha of the view |
2 | android:background | Specifies the background of the view |
3 | android:padding | Specifies padding of the view for all edges |
4 | android:tooltipText | Specifies text displayed in a small popup window on hover or long press |
5 | android:clickable | Specifies whether view is clickable or not |
6 | android:theme | Specifies a theme override for view |
7 | android:id | Specifies id of the view |
8 | android:padding | Specifies padding of the view |
Example of Android TabHost Widget
At first, we will create android application. Then, we will use TabHost widget in this application.
1. Creating New Project
Follow steps below to create new project. Please ignore the steps if you have already created a new application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as TabHost. Then, 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. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
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
Now, we will create tab pages that will be shown when any tab label is clicked. In this application, we are going to implement three tabs. We will create three activity that represents three pages. Follow the steps below to create pages (Home, Profile and Recent Pages) –
(a) HomeActivity.java and xml file (activity_main.xml)
(b) RecentActivity.java and xml file (activity_recent.xml)
(c) ProfileActivity.java and xml file (activity_profile.xml)
3.1 Create First Page(Home Page) of TabHost
Create a new java file in main/java/com.tutorialwing.tabhost folder and name it HomeActivity.java. Then, add below code into it.
package com.tutorialwing.tabhost; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { 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 java file in main/java/com.tutorialwing.tabhost folder and name it RecentActivity.java. Then, add below code into it.
package com.tutorialwing.tabhost; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class RecentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { 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 java file in main/java/com.tutorialwing.tabhost folder and name it ProfileActivity.java. Then, add below code into it.
package com.tutorialwing.tabhost; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class ProfileActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { 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 Layout. TabWidget contains the tab labels which displays page of selected tab in frameLayout when clicked.
5. Access TabHost Widget in java file
Open src/main/java/com.tutorialwing.tabhost/MainActivity.java 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; public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost tabHost = findViewById(android.R.id.tabhost); if (tabHost != null) { addTab(tabHost, getString(R.string.home), getString(R.string.home), HomeActivity.class); addTab(tabHost, getString(R.string.recent), getString(R.string.recent), RecentActivity.class); addTab(tabHost, getString(R.string.profile), getString(R.string.profile), ProfileActivity.class); tabHost.setCurrentTab(1); tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { Toast.makeText(getApplicationContext(), tabId, Toast.LENGTH_SHORT).show(); } }); } } private void addTab(TabHost tabHost, String name, String indicator, Class<?> className) { TabHost.TabSpec tabSpec = tabHost.newTabSpec(name); tabSpec.setIndicator(indicator); Intent intent = new Intent(this, className); tabSpec.setContent(intent); tabHost.addTab(tabSpec); } }
In MainActivity.java file, we have accessed tabHost. Then, we have added three tabs(Home, Profile and Recent) in it. After that, we have set tab change listener to show a toast message that displays current 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 widget.
You must be logged in to post a comment.