reiley
reiley

Reputation: 3761

Get instance of child/sub TabHost from the parent TabHost

I've created an activity which contains TabHost. In one of the tabspec there is another TabHost (like a sub-TabHost).

By default the visibility of this subTabHost is gone, & is only visible when the second parent tabspec is selected.

Now when the second tab is selected, I want to get the instance of the subTabHost inside java code in TabSpec variable.

Thank You

Layout:

Parent TabHost Layout

<TabHost
        android:id="@android:id/tabhost"
        android:visibility="gone" >
        ...

            <TabWidget
                android:id="@android:id/tabs" />

            <FrameLayout
                android:id="@android:id/tabcontent" >

                <include
                    android:id="@+id/abc"
                    layout="@layout/abc"
                    android:visibility="gone" />

                <include
                    android:id="@+id/subtab2"
                    layout="@layout/subtab2"   <--! sub tab -->
                    android:visibility="gone" />
        ... 


Inside layout of subtab2
<!-- want to get this's tabhost instance in code -->
<TabHost 
        android:id="@android:id/tabhost"
        android:visibility="visible" >

        <LinearLayout
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs" />

            <FrameLayout
                android:id="@android:id/tabcontent" >

                <include
                    android:id="@+id/xyz"
                    layout="@layout/xyz"
                    android:visibility="gone" />

        .....

Java Code

TabHost parent =  mTabHost = (TabHost) findViewById(android.R.id.tabhost);
// How to code below
(if subtab2 is visible)
Tabhost subTabHost = ??

Upvotes: 0

Views: 662

Answers (1)

user
user

Reputation: 87064

As you used the same ids for the TabHosts you can't use findViewById(android.R.id.tabhost) in the main layout but you can use it in the content panel:

// when the second tab is selected
FrameLayout content = (FrameLayout) findViewById(android.R.id.tabcontent); // main tabs content
TabHost subtabs = (TabHost)content.findViewById(android.R.id.tabhost); 

Upvotes: 2

Related Questions