Monday, October 4, 2010

Easy Tab Layout using separate Activities to fill Tab Content.

This is the working tab layout that I have designed thus far. This project requires three xml layouts, two xml drawables and a three java classes (and two png drawables for tab logos).


The main.xml file should have the following. Elements like buttons or TextViews that are needed to be displayed outside of the tabs or tab window should be placed above or below TabHost. Use the include method to add tab content layout files.


main.xml
<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" 
  android:isScrollContainer="true">

<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
     android:layout_height="fill_parent">
<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="fill_parent" 
  android:layout_height="fill_parent"><
/FrameLayout>
</LinearLayout>
</TabHost>
I'm only going to include two tabs. You need to have a separate layout.xml file for each tab's content. 


tab1.xml and tab2.xml (they are the same for the sake of this tutorial)




<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/scheduler_dayview" 
  android:layout_height="fill_parent" 
  android:layout_width="fill_parent" 
  android:orientation="vertical">

<TextView android:layout_width="fill_parent" 
  android:text="Tab 1" 
  android:padding="5px" 
  android:id="@+id/TextView01" 
  android:layout_height="wrap_content"
</TextView>

</LinearLayout>
Again, for the sake of this tutorial, the tabs respective activities are the same. 

Tab1.java and Tab2.java




public class Tab1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
        



   }

}

Now for the java class.

TabLayoutExample.Java

public class TabLayoutExample extends TabActivity {

    // Called when the activity is first created. 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Tab Stuff
        
Resources res = getResources(); 
        TabHost tabHost = getTabHost();  
        Intent intent;  

        // Create an Intent to launch an Activity for the tab 
        intent = new Intent().setClass(this, Tab1.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        tabHost.addTab(tabHost.newTabSpec("t1").setIndicator("Tab1",
                          res.getDrawable(R.drawable.tab1))
                      .setContent(intent));

        // Do the same for the other tabs
        intent = new Intent().setClass(this, Tab2.class);
        tabHost.addTab(tabHost.newTabSpec("t2").setIndicator("Tab2",
                          res.getDrawable(R.drawable.tab2))
                      .setContent(intent));

        tabHost.setCurrentTab(0);
    }
}

No comments:

Post a Comment