Use TabHost

Description

The following code shows how to use TabHost.

Example

Add activity to the manifest xml file.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.java2s.android.activities"
      android:versionCode="1"
      android:versionName="1.0.0">
  <uses-permission android:name="android.permission.INTERNET" />
  <application android:label="@string/app_name">
    <activity android:name=".IntentTabDemo"
          android:label="@string/app_name">
      <intent-filter>// w  w  w .  j a va 2  s  .  c om
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".MyBrowser" />
    <activity android:name=".AndroidBrowser" />
  </application>
</manifest>

Java code


import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TabHost;
//  w ww .  j  a va  2s .c o  m
public class IntentTabDemo extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TabHost host=getTabHost();
    
    host.addTab(host.newTabSpec("one")
            .setIndicator("CW")
            .setContent(new Intent(this, MyBrowser.class)));
    host.addTab(host.newTabSpec("two")
            .setIndicator("Android")
            .setContent(new Intent(this, AndroidBrowser.class)));
  }
}



import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;

public class MyBrowser extends Activity {
  WebView browser;
    
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    
    browser=new WebView(this);
    setContentView(browser);
    browser.loadUrl("http://java2s.com");
  }      
}


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;

public class AndroidBrowser extends Activity {
  WebView browser;
    
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    
    browser=new WebView(this);
    setContentView(browser);
    browser.loadUrl("http://code.google.com/android");
  }      
}




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView