Waypoint
Waypoint

Reputation: 17743

Android - making subtabs wit TabHost

is it possible to create subtabs in tabs made by TabHost? If so, how? I was not able to find any valuable source or help in this case.

Thanks

Upvotes: 0

Views: 892

Answers (1)

kaspermoerch
kaspermoerch

Reputation: 16570

It is possible to put a TabActivity inside a Tab.

Say you have MainTabActivity with two Tabs. First Tab can then hold FirstSubTabActivity and the second Tab could hold SecondSubTabActivity.

Here is an example:

Main activity:

public class MainTabActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass( this, FirstSubTabActivity.class );
        spec = tabHost.newTabSpec( "FirstTab" ).setIndicator( "One" ).setContent( intent );
        tabHost.addTab( spec );

        intent = new Intent().setClass( this, SecondSubTabActivity.class );
        spec = tabHost.newTabSpec( "SecondTab" ).setIndicator( "Two" ).setContent( intent );
        tabHost.addTab( spec );
    }
}

First subactivity:

public class FirstSubTabActivity extends TabActivity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       TabHost tabHost = getTabHost();
       TabHost.TabSpec spec;
       Intent intent;

       intent = new Intent().setClass( this, SomeActivity.class );
       spec = tabHost.newTabSpec( "SubTab" ).setIndicator( "One" ).setContent( intent );
       tabHost.addTab( spec );

       intent = new Intent().setClass( this, SomeOtherActivity.class );
       spec = tabHost.newTabSpec( "AnotherSubTab" ).setIndicator( "Two" ).setContent( intent );
       tabHost.addTab( spec );
   }
}

Second subactivity:

public class SecondSubTabActivity extends TabActivity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       TabHost tabHost = getTabHost();
       TabHost.TabSpec spec;
       Intent intent;

       intent = new Intent().setClass( this, SomeThirdActivity.class );
       spec = tabHost.newTabSpec( "ThirdSubTab" ).setIndicator( "One" ).setContent( intent );
       tabHost.addTab( spec );

       intent = new Intent().setClass( this, SomeFourthActivity.class );
       spec = tabHost.newTabSpec( "FourthSubTab" ).setIndicator( "Two" ).setContent( intent );
       tabHost.addTab( spec );
   }
}

Upvotes: 2

Related Questions