9 Jan 2012

Example: TabHost in Android

main.xml
<?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="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" >

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

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

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

</LinearLayout>
Activity.java
package test.uitestproject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

public class UITestProjectActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
        
        tabHost.setup();
        
        TabHost.TabSpec spec;
        
        spec = tabHost.newTabSpec("Tab 00");
        spec.setIndicator("Project");
        spec.setContent(R.id.tab1);
        tabHost.addTab(spec);
        
        spec = tabHost.newTabSpec("Tab 01");
        spec.setIndicator("Task");
        spec.setContent(R.id.tab2);
        tabHost.addTab(spec);
        
        tabHost.setCurrentTab(0);
    }
}

0 comments:

Post a Comment