Monday, April 11, 2011

TABS IN ANDROID

In order to create the tabs we have to use the following
TabHost
TabWidget
The TabHost is the root node for the layout contain TabWidget and Frame Layout.
The TabWidget is to maintain the tabs.
The Frame Layout contains the visible view for that Tab.

tabs.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">
   
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       
        <!-- Contain Tabs -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
       
        <!-- Contain Content of the Tab -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
           
    </LinearLayout>
</TabHost>

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>




Home.java
package com.tabsdemo.activities;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;


public class Home extends TabActivity
{
    /** Called when the activity is first created. */
    TabHost objTabHost;
    public String TAB_NAMES[] = {"tab1","tab2","tab3"};
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);
       
        //Getting the TabHost Object to add TabSpec
        objTabHost = getTabHost();
       
        for(int i=0 ; i
        {
            //Creation of a Tab
            TabHost.TabSpec tab = objTabHost.newTabSpec(TAB_NAMES[i]);
           
            //Associating Intent to Tab
            ComponentName oneActivity = new ComponentName("com.tabsdemo.activities", "com.tabsdemo.activities." + TAB_NAMES[i]);
            Intent intent = new Intent().setComponent(oneActivity);                       
            tab.setContent(intent);
           
            //Adding Name and Drawable to Tab
            tab.setIndicator(TAB_NAMES[i], getResources().getDrawable(R.drawable.more_tab));//("TAB"+i, R.drawable.more_tab);
           
            objTabHost.addTab(tab);
        }
       
        objTabHost.setCurrentTab(0);
    }
}


tab1.iava:
 package com.tabsdemo.activities;

import android.app.Activity;
import android.os.Bundle;

public class tab1 extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Content of the Tab1
    }
}

tab2.java:
 package com.tabsdemo.activities;

import android.app.Activity;
import android.os.Bundle;

public class tab2 extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
tab3.java:
package com.tabsdemo.activities;

import android.app.Activity;
import android.os.Bundle;


public class tab3 extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

If you want  more detail go to the following link:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

No comments:

Post a Comment