Android UI Tutorial - Android TabHost








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>
        <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;
/*from w ww  .  jav a2s .  co  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");
  }      
}