Add Tab to TabHost : TabHost « UI « Android






Add Tab to TabHost

   

package app.test;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class Test extends TabActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("TabDemoActivity");
    TabHost tabHost = getTabHost();
    LayoutInflater.from(this).inflate(R.layout.main,
        tabHost.getTabContentView(), true);
    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1")
        .setContent(R.id.view1));
    tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2")
        .setContent(R.id.view2));
    tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
        .setContent(R.id.view3));
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/view1"
        android:background="@drawable/icon"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Tab1"/>

    <TextView android:id="@+id/view2"
        android:background="@drawable/icon"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Tab2"/>

    <TextView android:id="@+id/view3"
        android:background="@drawable/icon"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Tab3"/>

</FrameLayout>

   
    
    
  








Related examples in the same category

1.Using TabHost
2.Using a tab content factory for the content via TabHost.TabSpec#setContent(android.widget.TabHost.TabContentFactory)
3.An example of tab content that launches an activity via android.widget.TabHost.TabSpec#setContent(android.content.Intent)
4.Dynamic Tab Demo
5.Tab control
6.Uses a right gravity for the TabWidget.