Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TabHost - Activities within each tab

I'm trying to create multiple Tabs, each with a different Activity. The only downside is i'm using a custom layout file thus my class extends an Activity rather than a TabActivity. While trying to run, it fails and suggests calling TabHost.Setup(ActivityGroupManager agm)

Anyone have an idea/practical example of how this can be achieved?

Thanks in advance

like image 546
Muniu Avatar asked Jun 23 '10 15:06

Muniu


1 Answers

This is a sample of my activity that also doesn't extend from TabActivity:

protected TabHost tabs;

// ...

/**
 * Init tabs.
 */
private void initTabs() {
    tabs = (TabHost) findViewById(R.id.tabhost);
    tabs.setup();
    tabs.setBackgroundResource(R.drawable.bg_midgray);

    TabHost.TabSpec spec;

    // Location info
    txtTabInfo = new TextView(this);
    txtTabInfo.setText("INFO");
    txtTabInfo.setPadding(0, 0, 0, 0);
    txtTabInfo.setTextSize(14);
    txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_inactive_right_inactive);
    txtTabInfo.setTextColor(Color.DKGRAY);
    txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    txtTabInfo.setHeight(39);
    spec = tabs.newTabSpec("tabInfo");
    spec.setContent(R.id.tabInfo);
    spec.setIndicator(txtTabInfo);
    tabs.addTab(spec);

    // Maps
    txtTabMap = new TextView(this);
    txtTabMap.setText("MAP");
    txtTabMap.setTextSize(14);
    txtTabMap.setPadding(0, 0, 0, 0);
    txtTabMap.setBackgroundResource(R.drawable.bg_tab_middle_inactive_right_active);
    txtTabMap.setTextColor(Color.DKGRAY);
    txtTabMap.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    txtTabMap.setHeight(39);
    spec = tabs.newTabSpec("tabMap");
    spec.setContent(R.id.tabMap);
    spec.setIndicator(txtTabMap);
    tabs.addTab(spec);

    tabs.setCurrentTab(0);

    tabs.setOnTabChangedListener(this);
}

// ...
like image 131
Mathias Conradt Avatar answered Sep 18 '22 18:09

Mathias Conradt