Thursday, July 26, 2012

Creating Tabs in Android




click here to download source code

Project Name     :  Tab1

Package Name   :  selva.tab

Version              :  2.1 ( Supports 2.1 and above versions)


main.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"
            android:padding="5dp">
                    <TabWidget
                       android:id="@android:id/tabs"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content" />
                   <FrameLayout
                      android:id="@android:id/tabcontent"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:padding="5dp" />
       </LinearLayout>
</TabHost>



tab1xml.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Hi.. welcome to tab1 !!!"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="100dp"
        android:text="You can add your form widgets and control here !!" />

</RelativeLayout> 



tab2xml.xml  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Hi.. welcome to tab2 !!!"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="100dp"
        android:text="You can add your form widgets and control here !!" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="150dp"
        android:text="You are in tab2 !!!!!!!!!!"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>



 select.xml    ( res/drawable/select.xml)


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/lightaccept"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/accept" />
   
</selector>



Tab1Activity.class


 package selva.tab;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class Tab1Activity extends TabActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
      
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

 
        intent = new Intent().setClass(this, Activity1.class);

    
        spec = tabHost.newTabSpec("artists").setIndicator("Tab 1",
                          res.getDrawable(R.drawable.select))
                      .setContent(intent);
        tabHost.addTab(spec);

  
        intent = new Intent().setClass(this, Activity2.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Tab 2",
                          res.getDrawable(R.drawable.select))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this,Activity3.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Tab 3",
                          res.getDrawable(R.drawable.select))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(2);
      
      
      
    }
}



 Activity1.class


package selva.tab;

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.tab1xml);
        
    }
   
}





 

 Activity2.class

package selva.tab;

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


public class Activity2 extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         setContentView(R.layout.tab2xml);
        
    }
   
}



Activity3.class


package selva.tab;

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


public class Activity3 extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
      
         TextView textview = new TextView(this);
            textview.setText("This is the third tab");
            setContentView(textview);
       
    }
   
}







OUTPUT  :







click tab1




click tab2 


































click here to download source code




1 comment: