Shy
Shy

Reputation: 401

Monodroid tabs view

After implementing the Tabs Widget Sample I tried to play with it and add the third tab only after changing to the second tab

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource  
        SetContentView(Resource.Layout.Main);

        TabHost.TabSpec spec;

        spec = TabHost.NewTabSpec("tab_test1").SetIndicator("TAB 1").SetContent(Resource.Id.textview1);
        TabHost.AddTab(spec);

        spec = TabHost.NewTabSpec("tab_test2").SetIndicator("TAB 2").SetContent(Resource.Id.textview2);
        TabHost.AddTab(spec);

        //spec = TabHost.NewTabSpec("tab_test3").SetIndicator("TAB 3").SetContent(Resource.Id.widget0);
        //TabHost.AddTab(spec);

        TabHost.TabChanged += new EventHandler<Android.Widget.TabHost.TabChangeEventArgs>(TabHost_TabChanged);

        TabHost.CurrentTab = 0;
    }

    void TabHost_TabChanged(object sender, TabHost.TabChangeEventArgs e)
    {
        if (TabHost.TabWidget.TabCount < 3)
        {
            TabHost.TabSpec spec;

            spec = TabHost.NewTabSpec("tab_test3").SetIndicator("TAB 3").SetContent(Resource.Id.widget0);
            TabHost.AddTab(spec);
        }
    }

The problem is that I see the 3rd view overlay-ed on the first view before clicking the tabs, even though the 3rd tab appears only after clicking the 2nd tab. What's going on?

Upvotes: 0

Views: 1250

Answers (1)

chrisntr
chrisntr

Reputation: 2208

I'm guessing it's because the Third tab doesn't have tab to go to (since we don't create a TabSpec) so it just displays it directly on the screen.

You could set the content you want to display when the third tab is visible to invisible shown in the example below;

<TextView 
    android:visibility="invisible"
    android:id="@+id/textview3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:text="this is a third tab" />

and then when the tab is displayed, the text view is made visible again.

Hope this helps,

ChrisNTR

Upvotes: 2

Related Questions