Search code examples
androidandroid-tabhostandroid-tabactivityandroid-tabs

How to deal with tabs as views?


I've created tabs as views. But I didn't find any info on how to manipulate theses views. For example if I want to display a listview in one and a form (tablelayout) in another. Can I have separate layouts for each tab? And more importantly where do I include java code regarding each tab?

Here's my java code:

public class TabWorkEntryActivity extends TabActivity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.tabworkentry);

TabHost  mTabHost = getTabHost();


  mTabHost.addTab(mTabHost.newTabSpec("top10").setIndicator("Top 10").setContent(R.id.Top_10));
  mTabHost.addTab(mTabHost.newTabSpec("billable").setIndicator("Billable").setContent(R.id.Billable));
  mTabHost.addTab(mTabHost.newTabSpec("product").setIndicator("Product").setContent(R.id.Product));
       mTabHost.addTab(mTabHost.newTabSpec("regular").setIndicator("Regular").setContent(R.id.General));

  mTabHost.setCurrentTab(3);


}

Any kinda help is appreciated.


Solution

  • There are two main ways of putting content and dealing with tabs: 1. you create an activity, and put the activity in the tabHost 2. you deal with the content, in the same code you add tabs (blow all the setup stuff) I always use the 2nd way because my codes are organized, so it's not so big...

    Here is a sample layout, with the content as another layout...

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TabHost
            android:id="@+id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_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/checkall_tab"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
    
                        <include
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            layout="@layout/checkall" />
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/view_tab"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
    
                        <include
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            layout="@layout/viewall" />
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/run_program"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
    
                        <include
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            layout="@layout/programs" />
                    </LinearLayout>
    
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    
    </LinearLayout>