SHIDHIN TS
SHIDHIN TS

Reputation: 1665

How to change tab content in android Tabhost?

I am having 3 Tabs Tab1,tab2, Tab3. In tab1 I am loading page1.java. In page1 there is a Next button. I need to change to page2.java while tap on the next button on Page1.java. I need to change only the content. I need the tabs there as permanent. Is it possible. I am doing like this on next button click,

Intent nextPageIntent = new Intent(context, Page2.class);
startActivity(nextPageIntent);

Please help me.

Upvotes: 2

Views: 5311

Answers (3)

sjain
sjain

Reputation: 23344

UPDATE:

For that reason you need a ActivityGroup within the Tab where you want to change the Activity.

    TabHost tabHost = getTabHost();

    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Page1").setContent(
            new Intent(this, Page1.class)));
    tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Page2").setContent(
            new Intent(this, Page2.class)));
    tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Page3").setContent(
            new Intent(this, Page3.class)));

The Page1 takes care which Activity is shown in the first tab. With setContentView you can bring another View to the front after an action is triggered.

    Intent intent = new Intent(context, Page2.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    View view = Page1.group.getLocalActivityManager().
                startActivity(intent).getPage2View();

    Page1.group.setContentView(view);

You need to tell the ActivityGroup which view is to be on top and set the appropriate Flag, so that the View will be on top.

Of course now you have to keep track which activity is in front, how they behave and what happens if the back button is pressed. But that gives you room for customizing the behavior.

More from: use-android-activitygroup-within-tabhost-to-show-different-activity.

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can try replacing fragment for that.

So keep all your pages (for one tab) in separate fragment. Load fragment 1 - i.e. Page 1 initially, and on click of Next Button, replace fragment like this:

nextBtn.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {

        Page2Fragment fragment = (Page2Fragment)myFragmentManager.findFragmentByTag(TAG_1);

        if (fragment == null) {

            //load page 2 in fragment
            FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.maincontainer, myFragment1, TAG_1);
            fragmentTransaction.commit();
        }
    }
});

Hope it helps.

Upvotes: 0

EatHeat
EatHeat

Reputation: 80

tabHost.setCurrentTab(2) should do it.

Upvotes: 0

Related Questions