Android: Tabhost Without Tabactivity

TabHost without extending TabActivity

With out extending the TabActivity, you can also implement the tab bar with the modifications in your xml file.


 <TabWidget
android:id="@android:id/tabs"

<FrameLayout
android:id="@android:id/tabcontent"

now intialize the tabhost and add tabs to your tabhost in this way.

  TabHost tab_host = (TabHost) findViewById(android.R.id.tabhost);

tab_host.setup();
// Set Tab Specification for Battery Tab

TabSpec battery_tab_spec = tab_host.newTabSpec("assignments_tab");

battery_tab_spec.setContent(R.id.assignments_tab);
battery_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
R.drawable.dashboard_assignments_student)));
tab_host.addTab(battery_tab_spec);

// Set Tab Specification for Network Tab
TabSpec network_tab_spec = tab_host.newTabSpec("discussions_tab");

network_tab_spec.setContent(R.id.discussions_tab);
network_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
R.drawable.dashboard_discuss)));
tab_host.addTab(network_tab_spec);

// Set Tab Specification for Device Tab
TabSpec device_tab_spec = tab_host.newTabSpec("progress_tab");

device_tab_spec.setContent(R.id.progress_tab);
device_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
R.drawable.dashboard_track)));
tab_host.addTab(device_tab_spec);

Try this.

TabHost without TabActivity, ActivityGroup, or LocalActivityManager

What is the correct way to have a TabHost and allow the tabs to contain Fragments without using deprecated functionality?

Put views in the tabs, whether statically in the layout as immediate children of the FrameLayout inside the TabHost, or dynamically via the variant of setContent() that takes a TabContentFactory. Those views can, AFAIK, take things that manage fragments (e.g., ViewPager), or possibly fragments directly themselves via <fragment> elements.

Or, don't use TabHost. Use tabs in the action bar, using ActionBarSherlock for backwards compatibility. Or, use a ViewPager. Or, use a ViewFlipper and a TabWidget (which, despite the docs, does not appear to have an actual dependency on TabHost). Or, use a ViewFlipper and some other control mechanism to switch between "tabs".

Or, get rid of your "fragment within the tab content".

How to display tab view without extending TabActivity

Activities in tabs was deprecated and Google agrees that it's a bad idea to do this, you have to consider using views as the contents of your Tabs, here an exemple from CommonsWare book you can find helpful resources.

Android - Tabhost working in Activity class

Ok I figured it out. Apparently, TabActivity extends ActivityGroup, which extends Activity. But in your code your class extends Activity which is not an activity group.

So there are two options:

1) If you want the tab content to be activities, have your class extend ActivityGroup (instead of Activity). Then your call to setup should be host.setup(getLocalActivityManager());

This way you are emulating the TabActivity source code.

2) If you can have your tab content be views (vs activities), keep your class as extending from Activity, and keep your call to setup(). But for the setContent part do something like this:

host.addTab(host.newTabSpec("two")
.setIndicator("Second Results")
.setContent(new TabContentFactory() {

public View createTabContent(String tag) {
return new TextView(TestActivity.this);
}
}));

And then define your list view inside createTabContent (that's usually what I do - I prefer using views instead of activities as the contents of the tabs).

How to add tab layout without letting the activity to extend TabActivity?

Put at least one tab content into it

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
tabHost.setup();

TabSpec tab1 = tabHost.newTabSpec("TAB_1");
tab1.setIndicator("Tab 1");
tab1.setContent(R.id.tab1);
tabHost.addTab(tab1);

//tab 2 etc...
TabSpec tab2 = tabHost.newTabSpec("TAB_2");
tab2.setIndicator("Tab 2");
tab2.setContent(R.id.tab2);
tabHost.addTab(tab2);
}

and in the xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">

<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>

<FrameLayout android:id="@android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/tab1">
<!-- tab 1 content goes here -->
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/tab2">
<!-- tab 2 content goes here -->
</LinearLayout>

</FrameLayout>

</LinearLayout>

</TabHost>

android TabHost inside a LinearLayout

Problem solved.
The correct solution, as given by the user 207 in one of the comments, from another stackoverflow question is this:

This is the XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost android:id="@android:id/tabhost"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="53px"
android:tabStripEnabled="false" android:layout_margin="1px"
android:background="#222222" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</LinearLayout>

and java file:

package eu.pkg.tabsExample;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TabHost;

public class TabsExampleActivity extends Activity {

TabHost tabHost;
LocalActivityManager mLocalActivityManager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initTabs(savedInstanceState);
}

@Override
protected void onResume() {
mLocalActivityManager.dispatchResume();
super.onResume();
}

@Override
protected void onPause() {
mLocalActivityManager.dispatchPause(isFinishing());
super.onPause();
}

private void initTabs(Bundle savedInstanceState) {
Resources res = getResources(); // Resource object to get Drawables
tabHost = (TabHost) findViewById(android.R.id.tabhost); // The activity TabHost
mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);

TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab

intent = new Intent("eu.pkg.tabsExample.BuildingTabActivity");
spec = tabHost.newTabSpec("text").setIndicator("",
res.getDrawable(R.drawable.tab_text_selector))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, ClipArtTabActivity.class);
spec = tabHost.newTabSpec("clipart").setIndicator("",
res.getDrawable(R.drawable.tab_clipart_selector))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, FrameSelectorTabActivity.class);
spec = tabHost.newTabSpec("frame").setIndicator("",
res.getDrawable(R.drawable.tab_frame_selector))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, BgSelectorTabActivity.class);
spec = tabHost.newTabSpec("bg").setIndicator("",
res.getDrawable(R.drawable.tab_bg_selector))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, BuildingTabActivity.class);
spec = tabHost.newTabSpec("build").setIndicator("",
res.getDrawable(R.drawable.tab_build_selector))
.setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(4);
}
}

the magic behind the scene is in those 4 lines of code:

    tabHost = (TabHost) findViewById(android.R.id.tabhost);     // The activity TabHost
mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);

Thank you all for contribution to this answer.



Related Topics



Leave a reply



Submit