Home > Android > Simple Tabhost in Android

Simple Tabhost in Android


Today, I will tell you how to use tabhost in android in a simple way.
First we will create a file and name it : /res/layout/tabhosttest.xml
The content of this file is:

<?xml version=”1.0″ encoding=”utf-8″?>
    android:id=”@+id/tabhost”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent” >
    <LinearLayout
        android:layout_width=”fill_parent”
        android:layout_height=”fill_parent”
        android:orientation=”vertical” >
        <TabWidget
            android:id=”@android:id/tabs”
            android:layout_width=”fill_parent”
            android:layout_height=”wrap_content” />
        <FrameLayout
            android:id=”@android:id/tabcontent”
            android:layout_width=”fill_parent”
            android:layout_height=”fill_parent” >
            <AnalogClock
                android:id=”@+id/tab1″
                android:layout_width=”fill_parent”
                android:layout_height=”fill_parent” />
            <Button
                android:id=”@+id/tab2″
                android:layout_width=”fill_parent”
                android:layout_height=”fill_parent”
                android:text=”A semi-random button” />
        </FrameLayout>
    </LinearLayout>
</TabHost>
And then we will create a file in /src/TabHostTest.class

package tabhost.com.vn;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class TabHostTutorialActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhosttest);
        TabHost tabs = (TabHost)findViewById(R.id.tabhost);
        tabs.setup();
        TabHost.TabSpec spec = tabs.newTabSpec(“tag1”);
        spec.setContent(R.id.tab1);
        spec.setIndicator(“Clock”);
        tabs.addTab(spec);
        spec = tabs.newTabSpec(“tag2”);
        spec.setContent(R.id.tab2);
        spec.setIndicator(“Button”);
        tabs.addTab(spec);
    }
}
Finally we have a user interface like this:
       
J
ust so simple! Hope you have fun with this. (^.^)!!!
Source : The Busy Coders Guide To Android Development Ebook
Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: