Creating Tab Bar using fragments from support package

Go To


Hello I am searching a good example creating my custom Tab Bar (at the bottom) using fragments.

Earlier I had written this using Activities Groups, but from android 3.* it's deprecated and I need to do this using fragments.

Here is the code I had written creating my custom Tab Bar:

private void setTabs()

    //Implementing strings
    String publication = String.format("First");
    String shop = String.format("Second");

    //Adding tabs to TabActivity
    //addTab(publication, R.drawable.ic_launcher, DisplayTestActivity.class);
   //addTab(shop, R.drawable.ic_launcher, DisplayPushedTestActivity.class);


public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub


 * @param labelId, tab name
 * @param drawableId, tab icon image
 * @param c, responsible class
private void addTab(String labelId, int drawableId, Class<?> c)
    TabHost tabHost = getTabHost();  // The activity TabHost

    Intent intent = new Intent(this, c);

    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);


    TextView title = (TextView) tabIndicator.findViewById(;

    ImageView icon = (ImageView) tabIndicator.findViewById(;


Maybe someone could share some info about this.


2012-04-04 05:46
by Streetboy


The developer website has a bunch of examples on how to do this. See, as well as the API 4+ Support Demos page.

This is a nice tutorial too if you need a step-by-step.

2012-04-04 06:03
by Alex Lockwood


I would try to integrate Actionbarsherlock into your project. It enables you to use the new TabApi from Honeycomb and Ice Cream Sandwich in all versions down to Android 2.x. It makes adding tabs really easy and your app will follow the Android Design Guidelines and really look like an Android App.

2012-04-04 06:14
by Janusz
I am already using Action Bar Sherlock and also want tab ba - Streetboy 2012-04-04 06:16