Taruni
Taruni

Reputation: 1671

How to add an activity to tab?

I have started working the customizing the Tabs in Tablayout, so i followed this tutorial http://joshclemm.com/blog/?p=136 which works fine. But the doubt is that i cant find out how to add an activity to each tab in this tutorial , i have tried but didnt find the solution.

In general we use

    TabSpec spec = tabHost.newTabSpec("Photos");
    spec.setIndicator("Photos");

    Intent photos = new Intent(this, Photos.class);
    spec.setContent(photos);

But dont know how to add an activity in this situation.

Can anybody help me get rid of this ?

Thanks.

Upvotes: 1

Views: 3893

Answers (4)

Maulik J
Maulik J

Reputation: 2765

    Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Resusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, DemoActivity1.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("todaystake").setIndicator("Todays Take",
                res.getDrawable(R.drawable.icontodaystake)).setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, DemoActivity2.class);
        spec = tabHost.newTabSpec("whatscasting").setIndicator(
                "What's Casting", res.getDrawable(R.drawable.iconwhatscasting))
                .setContent(intent);
        tabHost.addTab(spec);
tabHost.setCurrentTab(0);

your xml file will look something like this

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs" />
        <TabWidget android:id="@android:id/tabs"
            android:layout_alignParentBottom="true" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</TabHost>

Upvotes: 1

tttrung43
tttrung43

Reputation: 1

You just only replace the word photos by other name of your name activity class. But remember that, you must register your activity in androidmanifest file.

Upvotes: 0

Ajay
Ajay

Reputation: 4926

See this example

Upvotes: 1

Heiko Rupp
Heiko Rupp

Reputation: 30944

You are on the right track - Photos.class just needs to be an activity.

See e.g. this TabActivity on how I did it with Zwitscher

Upvotes: 0

Related Questions