views:

57

answers:

2

I have been reading a lot of comments on how it is more lightweight to use views instead of intents in setContent when using tabs.

Since I will be creating an app with tabs, I am trying to implement this instead of having intents inside setContent. However, I am having a difficult time looking for examples or straightforward tutorials on swapping the current view with another one.

Say I have TAB_A, with some entry fields and a button. When the user clicks on the button, a TextView will display "Hello, user!" in the same TAB_A. Of course this is an oversimplified example, but it will surely point me in the right direction.

EDIT: I have a tab called TAB_A with a button in it. If the user clicks on that button, TAB_A will now display "Hello, user!" instead of the button.

Here is a sample of my TabSpec.

TabHost.TabSpec spec = tabHost.newTabSpec(getString(R.string.tabspec_tag_search))
                .setIndicator(getString(R.string.tabspec_indic_search),
                        res.getDrawable(R.drawable.tab_ic_search))
                .setContent(new TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                   switch (doWhat){
                    case ACTIVITY_SPLASH_PAGE:
                                       return showButton;
                    case ACTIVITY_HELLO:
                                       return showHello;
                   }
                       return null;
                }
            });
        tabHost.addTab(spec);

Where ShowButton and ShowHello are classes both extending LinearLayout. Both are initialized in onCreate like this: ShowButton showButton = new ShowButton(this);

Inside the showButton class, I have this listener:

viewHolder.button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, Search247.class);
        intent.putExtra(EXTRA_DO_WHAT, ACTIVITY_HELLO);
        context.startActivity(intent);
    }
});

When the app first starts, the user sees the view showButton. When the user clicks on the button, I want the tab to change its contents to showHello.

The question then is: Is there any way to update the contents of the tabspec without using startActivity?

Thanks as always!

A: 

Found the answer:

ViewGroup viewGroup = (ViewGroup) mView.getParent();
viewGroup.removeView(mView);
addView(showResults);

Where mView is the initial view in the tab. Now just trying to figure out how to make the app remember to show mView when BACK is pressed from the new view.

Zarah
A: 

I have a similar issue.I have a tab as shown below where the content of a tab comes from the FlightsActivity calls.Inside the flights activity I have a form which when submitted need to change the content of the tab with a list view from another class.How could I remove the contents of the existing tab, and replace it with new contents.

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, FlightsActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec1 = tabHost
                .newTabSpec("flights")
                .setIndicator("Flights",
                        res.getDrawable(R.drawable.ic_tab_flights))
                .setContent(intent);
        tabHost.addTab(spec1);
syam