114 Java Android Program to Demonstrate Tab Navigation in Android
Posted by Superadmin on February 28 2017 12:53:25

Java Android Program to Demonstrate Tab Navigation in Android

Here is source code of the Program to Demonstrate Tab Navigation in Android. 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

<?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>