Tab Activity Example in android

As we know Tab-Activity is now depreciated in Android, but some time we use it for create simple Tab pages in our application. So today I am going to share tutorial for Tab Host Activity in android. It is a simple tab activity demo, group child activity and pager tab example I will share soon. Hope my blog help you. Please follow step by step my Activity for create simple Tab Layout....

1. Tab_Activity.java

package com.tutorialsee;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class Tab_Activity extends TabActivity implements OnTabChangeListener{

TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_tab);
Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    Log.e("w", String.valueOf(width));
    Log.e("h", String.valueOf(height));
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost = getTabHost();
   
    tabHost.setOnTabChangedListener(this);
      
        TabSpec find = tabHost.newTabSpec("find");
        find.setIndicator("");
        Intent homeIntent = new Intent(this, MainActivity.class);
        find.setContent(homeIntent);
       
     
        TabSpec fare = tabHost.newTabSpec("fare");
        fare.setIndicator("");
        Intent messageIntent = new Intent(this, FirstActivity.class);
        fare.setContent(messageIntent);
       
   
        TabSpec comm = tabHost.newTabSpec("comment");
        comm.setIndicator("");
        Intent sellIntent = new Intent(this, SecondActivity.class);
        comm.setContent(sellIntent);
       
     
        TabSpec sos = tabHost.newTabSpec("sos");
        sos.setIndicator("");
        Intent favoriteIntent = new Intent(this, FullActivity.class);
        sos.setContent(favoriteIntent);
       
        tabHost.addTab(find);         // Adding Home tab
        tabHost.addTab(fare);        // Adding messgae tab
        tabHost.addTab(comm);         // Adding sell tab
        tabHost.addTab(sos);  
       
        int r=(width/4);
       
        Log.e("r  "+r, " width "+width);
        if(width == 480){
        tabHost.getTabWidget().getChildAt(0).setLayoutParams(new
      LinearLayout.LayoutParams((width/4),120));
            tabHost.getTabWidget().getChildAt(1).setLayoutParams(new
            LinearLayout.LayoutParams((width/4),120));
            tabHost.getTabWidget().getChildAt(2).setLayoutParams(new
            LinearLayout.LayoutParams((width/4),120));
            tabHost.getTabWidget().getChildAt(3).setLayoutParams(new
            LinearLayout.LayoutParams((width/4),120));
         
        }else if(width == 120){
        tabHost.getTabWidget().getChildAt(0).setLayoutParams(new
      LinearLayout.LayoutParams((width/4),148));
            tabHost.getTabWidget().getChildAt(1).setLayoutParams(new
            LinearLayout.LayoutParams((width/4),148));
            tabHost.getTabWidget().getChildAt(2).setLayoutParams(new
                LinearLayout.LayoutParams((width/4),148));
            tabHost.getTabWidget().getChildAt(3).setLayoutParams(new
                LinearLayout.LayoutParams((width/4),148));
           
        }else if(width == 120){
          tabHost.getTabWidget().getChildAt(0).setLayoutParams(new
            LinearLayout.LayoutParams((width/4),80));
                  tabHost.getTabWidget().getChildAt(1).setLayoutParams(new
                  LinearLayout.LayoutParams((width/4),80));
                  tabHost.getTabWidget().getChildAt(2).setLayoutParams(new
                      LinearLayout.LayoutParams((width/4),80));
                  tabHost.getTabWidget().getChildAt(3).setLayoutParams(new
                      LinearLayout.LayoutParams((width/4),80));
                 
              }else if(width == 180){
        tabHost.getTabWidget().getChildAt(0).setLayoutParams(new
                    LinearLayout.LayoutParams((width/4),198));
            tabHost.getTabWidget().getChildAt(1).setLayoutParams(new
                    LinearLayout.LayoutParams((width/4),198));
            tabHost.getTabWidget().getChildAt(2).setLayoutParams(new
                LinearLayout.LayoutParams((width/4),198));
            tabHost.getTabWidget().getChildAt(3).setLayoutParams(new
                LinearLayout.LayoutParams((width/4),198));
          
        }else{
        tabHost.getTabWidget().getChildAt(0).setLayoutParams(new
                LinearLayout.LayoutParams((width/4),120));
        tabHost.getTabWidget().getChildAt(1).setLayoutParams(new
                LinearLayout.LayoutParams((width/4),120));
        tabHost.getTabWidget().getChildAt(2).setLayoutParams(new
      LinearLayout.LayoutParams((width/4),120));
        tabHost.getTabWidget().getChildAt(3).setLayoutParams(new
        LinearLayout.LayoutParams((width/4),128));
     
        }
        tabHost.getTabWidget().setStripEnabled(false);
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.call);
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.done);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.microphone);
        tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.search);
       
       
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.call);
}
@Override
public void onTabChanged(String tabId) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if(i==0)
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.call);
else if(i==1)
         tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.done);
else if(i==2)
         tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.microphone);
else if(i==3)
         tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.search);

}

Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
if(tabHost.getCurrentTab()==0)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.call);
else if(tabHost.getCurrentTab()==1)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.done);
else if(tabHost.getCurrentTab()==2)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.microphone);
else if(tabHost.getCurrentTab()==3)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.search);
}
}

2. activity_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true" >
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:dividerPadding="0dp"
            android:tabStripEnabled="true" >
        </TabWidget>
    </RelativeLayout>

</TabHost>