Monday, 8 October 2012

how create tabs in android application

use eclipse to create a project which contain tabs.
*select new project ,then select new android application project 
*chose the api level  i have taken 2.2
now in the layout folder go to mainpage.xml and paste this code 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  
 >


<TabHost
    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" >
        
   

        
   

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>

now in order  to create the tabs according to our requirement we can write java code given below




import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;



public class TabActivity extends  TabActivity 
{
public static TabActivity context;

 
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.mainpage);
       

       

          context=this;
       final TabHost tabHost = getTabHost();   
       Resources res = getResources();
   
  
     
     tabHost.addTab(tabHost.newTabSpec("tab1")
             .setIndicator("My Contact")
             .setContent(new Intent(this,TabGroup1Activity.class)));
     
     tabHost.addTab(tabHost.newTabSpec("tab2")
             .setIndicator("My Profile")
             .setContent(new Intent(this, TabGroup2Activity.class)));
     
     
     tabHost.addTab(tabHost.newTabSpec("tab2")
             .setIndicator("Settings")
             .setContent(new Intent(this, TabGroup3Activity.class)));
     
     
     tabHost.setCurrentTab(0); 
     for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
           tabHost.getTabWidget().getChildAt(i).getLayoutParams().height /=1.5;
          

       }
     
     
     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
int j=tabHost.getTabWidget().getChildCount();
Log.e("child countttttttttttttttttttttt ", Integer.toString(j));
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.BLACK);
tv.setTypeface(null,1);
tv.setTextSize(12);
 
//calling the method when the user change the tab
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
// int c=0;
// @Override
public void onTabChanged(String tabId) {
setTabColor(tabHost);
}
});
}
        setTabColor(tabHost);
        

}
public static void setTabColor(TabHost tabHost) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
try{
//  unselected tab will be displayed drawable background
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.header);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
           //tv.setTextColor(Color.parseColor("#909598"));
           tv.setTextColor(Color.parseColor("#7c8286"));
}
catch (NullPointerException e) {
// TODO: handle exception
e.printStackTrace();
}

}
try{
//user selected tab will be displayed selected color
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor( "#1082ef"));
//tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(R.drawable.hover);
TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
       tv.setTextColor(Color.parseColor("#ffffff"));
}
catch (NullPointerException e) {
// TODO: handle exception
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
       
}


   }


No comments:

Post a Comment