Sunday, 4 May 2014

How to create Tabview layout in Android

Tab view Layout

Create Tab_View.java

public class Tab_View extends TabActivity implements OnTabChangeListener {

TabHost tabHost;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab__view);
   
     // Get TabHost Refference
     tabHost = getTabHost();
   
     // Set TabChangeListener called when tab changed
     tabHost.setOnTabChangedListener(this);

     TabHost.TabSpec spec;
     Intent intent;

      /** TAB1 **/
     // Create  Intents to launch an Activity for the tab (to be reused)
     intent = new Intent().setClass(this, Tab_View1.class);
 
     spec = tabHost.newTabSpec("First").setIndicator("")
                   .setContent(intent);
   
     //Add intent to tab
     tabHost.addTab(spec);

     /** TAB2 **/
     intent = new Intent().setClass(this, Tab_View2.class);
 
     spec = tabHost.newTabSpec("Second").setIndicator("")
                   .setContent(intent);
     tabHost.addTab(spec);

     /** TAB3 **/
     intent = new Intent().setClass(this, Tab_View3.class);
 
     spec = tabHost.newTabSpec("Third").setIndicator("")
                   .setContent(intent);
     tabHost.addTab(spec);

     /** TAB4 **/
     intent = new Intent().setClass(this, Tab_View4.class);
 
     spec = tabHost.newTabSpec("Third").setIndicator("")
                   .setContent(intent);
     tabHost.addTab(spec);
   
     // Set drawable images to tab
 tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2);
 tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3);
 tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4);
   // Set Tab1 as Default tab and change image
     tabHost.getTabWidget().setCurrentTab(0);
     tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1_over);    
}
@Override
public void onTabChanged(String tabId) {
   
    /** Called when tab changed **/
   
//** Check current selected tab and change according images **/
   
   for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
    if(i==0)
       tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab1_over);
    else if(i==1)
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab2_over);
    else if(i==2)
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab3_over);
    else if(i==3)
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab4_over);
   
   }  
 
   Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
 
   if(tabHost.getCurrentTab()==0)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab1);
   else if(tabHost.getCurrentTab()==1)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab2);
   else if(tabHost.getCurrentTab()==2)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab3);
   else if(tabHost.getCurrentTab()==3)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab5);
 
}

}



 Create four class files Tab_View1.java,Tab_View2.java,Tab_View3.java,Tab_View4.java

Enjoy........

 


No comments:

Post a Comment