How to use tabwidget with fragments

This is a simple tutorial on how to use tabs with fragment in android application.

Final output

final output

In this application we are using fragment from android-support-v4.jar,  you are required to add android-support-v4.jar into your app.

1] Create a class lets say “BaseActivity” as we are using fragments to show tabs. BaseActivity class will extends “” class.

Here is xml file for BaseActivity class

 android:layout_height="match_parent" >

android:orientation="vertical" >

android:orientation="horizontal" />

android:layout_height="match_parent" />


2] BaseActivity class

In onCreate() method first setup tabhost instance and then add tabs.

mTabHost = (FragmentTabHost) findViewById(;

mTabHost.setup(this, getSupportFragmentManager(),;

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(getResources().getString(R.string.tab_one)), TabOneFrgment.class, null);

mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(getResources().getString(R.string.tab_two)), TabTwoFrgment.class, null);

Here we are adding tabs in tabhost using addTab method

addTab method takes 3 parameter:

a] TabSpec instance :  you can define tab indicator, can assign a tag for this tab etc.

b] activity which you want to show when user clicks on this tab.

c] bundle instance

  • setIndicator(): here you can set text and or icon which will displayed on tab.

  • You can also add icon with tabs

 Ex : mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(getResources().getString(R.string.tab_two),

                getResources().getDrawable(R.drawable.ic_launcher)),TabTwoFrgment.class, null);

Tag a tab

By default tabs are shown in the same order as they were added in tabhost.

You can set a tab as current tab  by using setCurrentTabByTag method.

Ex : mTabHost.setCurrentTabByTag("tag2");

You can also show a tab by using tags :

Ex : TabOneFrgment tabOneFrgment = (TabOneFrgment) fragmentManager.findFragmentByTag("tab1");;


How to navigate tabs???

Tab navigation has 2 aspects:

a] create a new fragment (add a fragment)

b] show already added fragment.

It is easy to create a new fragment on tab click but the trickiest part is to retain fragment state while changing tabs.

So in the sample app we are retaining fragment state whenever user changes a tab.

For this you need to add setOnTabChangedListener to the tab host.

Ex : mTabHost.setOnTabChangedListener(new OnTabChangeListener() {.....});

Inside setOnTabChangedListener we need check whether the intended fragment is already created (please refer fragment life cycle).

If fragment is already created  we just need to show that fragment else we will have to create a new fragment and hide current fragment.

 mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

 public void onTabChanged(String tabId) { fragmentManager = getSupportFragmentManager();

TabOneFrgment tabOneFrgment = (TabOneFrgment) fragmentManager.findFragmentByTag("tab1");

TabTwoFrgment tabTwoFrgment = (TabTwoFrgment) fragmentManager.findFragmentByTag("tab2"); fragmentTransaction = fragmentManager.beginTransaction();

if(tabOneFrgment != null){

if(tabTwoFrgment != null){





if(tabTwoFrgment != null){

if(tabOneFrgment != null){








Download sample project from here.
Happy Coding Happy Learning :)