Skip to content Skip to sidebar Skip to footer

Android: How Do You Set Up The Tabhost

I am trying to use the tabhost when I add it to my xml it doesn't look right, and I believe there is something that needs to be done in java, I am trying to set up three tabs with

Solution 1:

Yes, it is possible.

You can specify each activity (Start an intent) for each tabs in the following manner

publicvoidonCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        res = getResources();
        tabHost = getTabHost();
        TabHost.TabSpec spec;


        intent = newIntent().setClass(this, CalendarActivity.class);
        spec = tabHost.newTabSpec("calendar").setIndicator("Calendar", res.getDrawable(R.drawable.ic_tab_calendar)).setContent(intent);
        tabHost.addTab(spec);

        intent = newIntent().setClass(this, ProfileActivity.class);
        spec = tabHost.newTabSpec("profile").setIndicator("Profile", res.getDrawable(R.drawable.ic_tab_profile)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
}

Each activity will have their own content layout views, therefore no need to worry about that in the main layout.

Your main XML layout will be small and simple as

<?xml version="1.0" encoding="utf-8"?><TabHostxmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="wrap_content"android:layout_height="wrap_content"/><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="5dp" /></LinearLayout></TabHost>

I think this is what you want.

Solution 2:

It is not possible to have a tab correspond to an activity. The purpose of tabs is to break up one activity into how ever many views if there is too much info display in one view(not to be confused with activity). Here however is how you set up TabHost: First start with an xml:

<?xml version="1.0" encoding="UTF-8"?><TabHostxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabhost"android:layout_width="fill_parent"android:layout_height="fill_parent" >
 -

    <LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content" />
 -

        <FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent" ><AnalogClockandroid:id="@+id/tab1"android:layout_width="fill_parent"android:layout_height="fill_parent" /><Buttonandroid:id="@+id/tab2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="A semi-random button" /></FrameLayout></LinearLayout></TabHost>

Sorry that's hard to read but if you copy it into eclipse you should be fine. And then the Java:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

publicclassTabDemoextendsActivity {
    @OverridepublicvoidonCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        TabHost tabs=(TabHost)findViewById(R.id.tabhost); //Id of tab host

        tabs.setup();

        TabHost.TabSpec spec=tabs.newTabSpec("tag1");//make a new tab

        spec.setContent(R.id.tab1);  //What is in the tab (not an activity but rather a view)
        spec.setIndicator("Clock"); //Name of tab
        tabs.addTab(spec); //Add it 

        spec=tabs.newTabSpec("tag2"); //Same thing here
        spec.setContent(R.id.tab2);
        spec.setIndicator("Button");
        tabs.addTab(spec);
    }
}

This is relatively simple an I am sure there is support in the android development website and of course just searching on Google. This code was copied out of Beginning Android 4 by Grant Allen and the book explains this topic in much greater detail. Good luck!

Post a Comment for "Android: How Do You Set Up The Tabhost"