Search This Blog

Saturday, November 26, 2011

Tabs in android programming

First of all make an layout of this Tab Activity

<?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="#181818">
    <!-- LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"-->
        <RelativeLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
         <!--  HorizontalScrollView 
        android:id="@+id/horizontalScrollView1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        -->
        
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            
            />
            <!--   /HorizontalScrollView-->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
            </RelativeLayout>
    
</TabHost>




Then JAVA file of the Tab is below :


package com.blacky.TabActivities;


import com.blacky.basictutorial.R;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;


public class TabtryActivity extends TabActivity {
    


/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabactivity);
        
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;   // Resusable TabSpec for each tab
        Intent intent;   // Reusable Intent for each tab
        Resources res = getResources();
       
        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Tab1.class);


        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Artists",res.getDrawable(R.drawable.ic_drawable_changes))
                      .setContent(intent);
        tabHost.addTab(spec);


        // Do the same for the other tabs
        intent = new Intent().setClass(this,Tab2.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.ic_drawable_changes))
                      .setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, Tab3.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Songs")
                     .setContent(intent);
       tabHost.addTab(spec);


       tabHost.setCurrentTab(0);
    }
}


According to that make the xml file of all the tabs separately and put the contant view in the java file of all the tab.