Android App development: Tabbed Activities

Some times we want to wrap multiple views in a single window and navigate throught them with a Tab Container. this can be done in Android using TabHost control

There are two ways to use a TabHost application in Android:

  1. Using the TabHost to navigate through multiple views within the same activity.
  2. Using the TabHost to navigate through Actual multiple Activities using intents.

we will explain both ways through this tutorial:

Anatomy of  Tabbed Application

An activity with a TabHost may look like this:


The Activity consists of:

  1. A TabHost: the root element of the layout.
  2. The TabHost wraps a TabWidget which represents the tab bar.
  3. The TabHost wraps a FrameLayout which wraps the contents of each tab.

There are some rules that we must stick to when using a Tabbed activity:

  1. If the activity is of type TabActivity [optional] then the TabHost must have the id @android:id/tabhost.
  2. The TabWidget must have the id @android:id/tabs.
  3. The FrameLayout must have the id @android:id/tabcontent.

now let’s see an example to an activity with multiple tabs:
this is the layout

<?xml version="1.0" encoding="utf-8"?>

    <TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <TabWidget
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs"
    />
     <FrameLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabcontent"
     >
     <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tab1"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
     <TextView
    android:layout_width="fill_parent"
    android:layout_height="100px"
    android:text="This is tab1"
    android:id="@+id/txt1"
    />

     </LinearLayout>

     <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab2"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
     <TextView
    android:layout_width="fill_parent"
    android:layout_height="100px"
    android:text="This is tab 2"
    android:id="@+id/txt2"
    />

     </LinearLayout>

      <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab3"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
     <TextView
    android:layout_width="fill_parent"
    android:layout_height="100px"
    android:text="This is tab 3"
    android:id="@+id/txt3"
    />

     </LinearLayout>
     </FrameLayout>

    </TabHost>

then in the code of our activity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Tab 1");

        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2");
        spec2.setContent(R.id.tab2);

        TabSpec spec3=tabHost.newTabSpec("Tab 3");
        spec3.setIndicator("Tab 3");
        spec3.setContent(R.id.tab3);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);

 }

is going to look like this:

  1. We create tabs using TabSpecs class.
  2. We set the title of each tab using TabSpecs.setIndicator() method.
  3. We set the content of each tab using TabSpecs.setContent() method.
  4. if you use TabActivity to as a base class to your activity, you do not need to call TabHost.Setup() method.

We can add an icon to the title of the tab like this:

TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.flash));

        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2",getResources().getDrawable(R.drawable.sun));
        spec2.setContent(R.id.tab2);

        TabSpec spec3=tabHost.newTabSpec("Tab 3");
        spec3.setIndicator("Tab 3",getResources().getDrawable(R.drawable.chart));
        spec3.setContent(R.id.tab3);

it will look like this:


we can also specify the indicator to be a view:

TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);
        TextView txt=new TextView(this);
        txt.setText("Tab 1");
        txt.setBackgroundColor(Color.RED);
        spec1.setIndicator(txt);

Setting the content of tabs:

we saw how to set the contents of tabs by specifying multiple layout resources to be displayed within the same activity.

what If we have multiple Activities in our application and we want to navigate between them using tabs ?
in this case we will have one activity as the root activity of the application. this activity will have the TabHost and will navigate to other activities using Intents.
Note: the root activity must inherit from TabActivity.

the root activity will have layout file like this:

<?xml version="1.0" encoding="utf-8"?>
    <TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <TabWidget
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs"
    />
     <FrameLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabcontent"
     >
     </FrameLayout>
    </TabHost>

the other activities will have a simple layout consisting of a TextView.

now to the code of the root activity

public class TabDemo extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost=getTabHost();
        // no need to call TabHost.Setup()        

        //First Tab
        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.sun));
        Intent in1=new Intent(this, Act1.class);
        spec1.setContent(in1);
        // Second Tab
        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2",getResources().getDrawable(R.drawable.chart));
        Intent in2=new Intent(this,Act2.class);
        spec2.setContent(in2);

        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
    }
}

and the activity will look like this:

Adding tabs at run-time:

we can add tabs to TabHost at run-time using TabSpec.setContent(TabContentFactory) method.

the TabContentFactory is an interface that requires the implementation of a callback method createTabContent(String tag) which returns the view to be added to the content of the tab.

so in the last example if we changed code that adds the content of the second tab to this:

TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.sun));

        spec1.setContent(new TabContentFactory() {

   @Override
   public View createTabContent(String tag) {
    // TODO Auto-generated method stub

    return (new AnalogClock(TabDemo.this));
   }
  });

the activity will look like this:

iPhone-Like Tabs:

to give the Android tabs the same look of the iPhone tabs we wrap the TabWidget and the FrameLayout in a RelativeLayout container and adding android:layout_alignParentBottom=”true” attribute to the TabWidget just like this:

<?xml version="1.0" encoding="utf-8"?>

    <TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TabWidget
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs"
    android:layout_alignParentBottom="true"
    />
     <FrameLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabcontent"
     >

     </FrameLayout>
    </RelativeLayout>
    </TabHost>

In this post we saw how to create an application with tabs, stay tuned next week for more Android Development posts.


8 Responses to “Android App development: Tabbed Activities”

  1. Luke Whitworth 18. Aug, 2011 at 12:47 pm #

    where do i enter this code? im new code in android and outside google app inventor p.s. i need a reply A.S.A.P :)

  2. devilcol 27. Apr, 2012 at 1:53 am #

    How to remove tab from tabhost?

    I want to remove a tab when click on it ,which is already showing.

    there is no method to remove tab like addtab

    does anybody know about it

    please reply A.S.A.P :)

  3. Anonymous 28. Feb, 2013 at 5:54 am #

    in third tab if i want to upload image how should i do?

  4. Jorari71 05. Apr, 2013 at 8:04 am #

    Why does this line
    spec = tabHost.newTabSpec(“artists”).setIndicator(“Artists”,res.getDrawable(R.drawable.tab_c_g)).setContent(intent);

    returns me the error: in the getting of the icons “tab_c_g” as the debugger says:
    activity_main cannot be resolved or is not a field

    What can I do?. My drawable (icon) is tab_c_g…

    I don’t know what to do.

Trackbacks/Pingbacks

  1. Android: How to Remove Selected Tab Highlight Color & On Press Highlight on TabWidget | Build Future Repository - 20. Apr, 2012

    [...] am now working with Android TabWidget. I build this TabWidget based on http://mobileorchard.com/android-app-development-tabbed-activities/ I’ve already add background to the TabWidget, but apparently the highlight of selected tab [...]

  2. On an Android tab, how to show the text under the image? | PHP Developer Resource - 29. May, 2012

    [...] I think the default behaviour is that (see some example here: http://mobileorchard.com/android-app-development-tabbed-activities/) [...]

  3. Application has stopped unexpectedly. (Because of tabView) : Android Community - For Application Development - 22. Apr, 2013

    [...] I’m trying to do an app which use Tabs. I follow this tuto: http://mobileorchard.com/android-app-development-tabbed-activities/ [...]

Leave a Reply

© 2008-2051 • Mobile Orchard and the Bar-Tree Logo are service marks. ContactAdvertise