How can i get the class below to create tabs that link to activities. its worrking the way it is but the tabs are not linked to any activity. I need the tabs to load different activities?
import android.app.TabActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
public class CustomTabActivity extends TabActivity {
TabHost mTabHost = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
// mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
No, you do not. You gain nothing from having your tabs populated with activities. In fact, doing that causes more harm than good — you waste heap space, stack space, and CPU time for no added value.
Now, you do not have to define your tab content at runtime if you do not want. For example, here is a sample project that defines the tab content in the same layout as the
TabHostitself.