How to Create Tab Navigation in Android?

Here is source code of the Program to Demonstrate Tab Navigation in Android using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

package com.example.tab_layout;
 
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
 
public class MainActivity extends TabActivity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        TabHost tabHost = getTabHost();
 
        TabSpec activity1 = tabHost.newTabSpec("Activity 1");
        // Tab for first activity
        activity1.setIndicator("Activity 1",
                getResources().getDrawable(R.drawable.icon_tab1));
        Intent intent1 = new Intent(this, Activity1.class);
        photospec.setContent(intent1);
 
        // Tab for First Activity
        TabSpec activity2 = tabHost.newTabSpec("Activity 2");
        activity2.setIndicator("Activity 2",
                getResources().getDrawable(R.drawable.icon_tab2));
        Intent intent2 = new Intent(this, Activity2.class);
        songspec.setContent(intent2);
 
        // Tab for Second Activity
        TabSpec activity3 = tabHost.newTabSpec("Activity 3");
        activity3.setIndicator("Activity 3",getResources().getDrawable(R.drawable.icon_tab3));
        Intent intent3 = new Intent(this, Activity3.class);
        activity3.setContent(intent3);
 
        tabHost.addTab(activity1);
        tabHost.addTab(activity2);
        tabHost.addTab(activity3);
    }
 
}

First Activity

package com.example.tab_layout;
 
import android.app.Activity;
import android.os.Bundle;
 
public class Activity1 extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
    }
 
}

Activity_Main

advertisement
advertisement
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/abhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
 
        <TabWidget
            android:id="@android:id/abs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
 
        <FrameLayout
            android:id="@android:id/abcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

Screenshot_2013-11-16-19-45-27

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.