Saturday, 9 February 2013

How To Add Tabs In Android

 This tutorial is based on adding tabs in your activity in android.Also you give background
  image to your tabs.So follows the step:

Step1) First you add the following code in your Xml file,This will have three basic components
                             a)Tab-Host     b)Frame-layout     c)Tab-widget

        tab_layout.xml

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_weight="1">

        <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:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="0dip"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:editable="false"
                    android:textSize="10sp" />

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </TabHost>
    </LinearLayout>         

 Step2)          
                  Then coming to the Java file you add the following code: 

          TabDemo.class 

        a)  Tabhost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        b) Add the tabspec to your Tabhost

             TabSpec firstTabSpec = tabHost.newTabSpec("Type1");
             TabSpec secndTabSpec = tabHost.newTabSpec("Type2");


        c)Set the action for the tabspec and background image for spec.
        
             
//Type1
              Intent i1=new Intent(ClassActivity.this,Type1Activity.class);
              firstTabSpec.setIndicator("",getResources().getDrawable(R.drawable.type1)).setContent(i1);
          
              //Type2
              Intent i2=new Intent(ClassActivity.this,Type2Activity.class);
              secndTabSpec.setIndicator("",getResources().getDrawable(R.drawable.type2)).setContent(i2);

                    
         d)Add the tab to the tabhost.
 
            
  tabHost.addTab(firstTabSpec);
               tabHost.addTab(secondTabSpec);




   Thnks...
          

   

0 comments:

Post a Comment

 

Copyright © Unique Android Tutorial.. Design by Free CSS Templates | Blogger Theme by BTDesigner | Powered by Blogger