Here is a basic example of a TabHost, which allows a user to create a tabs view (the default android ‘contacts/dialer’ menu uses tabhost).

First, the code:

package com.android.mytabviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class tabViewTest2 extends Activity {

private int ADD_NEW_TAB = Menu.FIRST;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost);
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec(”First Tab”);
tspec1.setIndicator(”One”);
tspec1.setContent(R.id.content);
tabs.addTab(tspec1);
TabSpec tspec2 = tabs.newTabSpec(”Second Tab”);
tspec2.setIndicator(”Two”);
tspec2.setContent(R.id.content);
tabs.addTab(tspec2);
TabSpec tspec3 = tabs.newTabSpec(”Third Tab”);
tspec3.setIndicator(”Three”);
tspec3.setContent(R.id.content);
tabs.addTab(tspec3);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0, ADD_NEW_TAB, 0, “New Tabs”);
return true;
}

//Dynamically delete tabs, then add one again
//Problem with SDK 1.1 returns null pointer exception
@Override
public boolean onOptionsItemSelected(MenuItem item) {

TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost);
tabs.clearAllTabs();
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec(”New Tab”);
tspec1.setIndicator(”NEWTAB”, this.getResources().getDrawable(R.drawable.icon));
tspec1.setContent(R.id.content);
tabs.addTab(tspec1);

return super.onOptionsItemSelected(item);
}
}

Now, the XML (i.e. main.xml):

<?xml version=”1.0″ encoding=”utf-8″?>
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/my_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=”65px”/>

<FrameLayout
android:id=”@android:id/tabcontent”
android:layout_width=”fill_parent”
android:layout_height=”200px”
android:paddingTop=”65px”>
<LinearLayout
android:id=”@+id/content”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
</LinearLayout>
</FrameLayout>
</TabHost>

For more information, follow this link to the documentation for a TabHost.

http://developer.android.com/reference/android/widget/TabHost.html