Skip to main content

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();
}
       
}


   }


Comments

Popular posts from this blog

Android ListView with Searchbox Sort items

This article is next step of List Example. Here we have a search box which sorts the list view as when the content matches the list items. Why do we need this? Imagine that if we have 2K to 3K items in the listview, It will not be possible to  scrolldown till 2000 th  item.In this case this will be handy to cut short items. To start with we add a edittext box and Listview added to LinearLayout. [sourcecode language="xml"] <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <EditText android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="Search"></EditText><ListView androi...

How to change textcolor of tab text in Android

How to change textcolor of tab text in Android TabHost tabs = (TabHost)findViewById(R.id.TabHost01);         tabs.setup(getLocalActivityManager());                       TabHost.TabSpec search = tabs.newTabSpec("tag1");         search.setContent(new Intent(this,Searchgurbani.class));         search.setIndicator("Find Me");         tabs.addTab(search);                TabHost.TabSpec pref = tabs.newTabSpec("tag5");         pref.setContent(new Intent(this,Preferencegurbani.class));         pref.setIndicator("Settings");         tabs.addTab(pref);                TabHost.TabSpec favorite =...

Class cast exception

This Exception mainly occur because when we create an action on click of a button or an action to be performed on start of an activity ,but we forget the write the code in the activity for which we wrote intent to go. for example in  a main.xml file we have 7 buttons. <ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"         android:layout_height="fill_parent"         >     <LinearLayout         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:orientation="vertical"         android:padding="30dp" >         <TextView             android:id="@+id/textView1"             android:layout_width="wrap_content"       ...