Start different activities in separate tabs of a TabHost

1 vote · 4 comments

There are times when you may want to divide multiple Activities into a tabhost. It should be noted that activities incur a much larger system footprint rather than just dividing one activity into multiple views. But you could choose to progromatically introduce activities to the tabhost using this technique. The full source of this example and others is available here: http://github.com/novoda/android/

raw ·
copy
· download
/*** * * Main class - Container.java * public class Container extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost host = getTabHost(); host.addTab(host.newTabSpec("one").setIndicator("TAB1").setContent(new Intent(this, Tab1Activity.class))); host.addTab(host.newTabSpec("two").setIndicator("TAB2").setContent(new Intent(this, Tab2Activity.class))); } } /*** * * Tab1Activity.java * public class Tab1Activity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab1); } } /*** * * Tab2Activity.java * public class Tab2Activity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab2); } } /*** * * res/layout/main.xml * <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> /*** * * res/layout/tab1.xml * <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="tab1" /> </LinearLayout> /**** * * res/layout/tab2.xml * <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="tab2" /> </LinearLayout> /*** * * res/values/strings.xml * <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Container!</string> <string name="app_name">MultipleActivityTabHost</string> </resources> /*** * * AndroidManifest.xml * <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.novoda" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="3" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Container" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Tab1Activity"></activity> <activity android:name="Tab2Activity"></activity> </application> </manifest>
Add a comment

4 Comments

Boy I was way off - thanks - I was trying to do the tab stuff within the xml file and here in this example you have nothing like that in the xml file you have it all in the java files. But of course it gave me problems - I had it as follows:

Reply · Sept. 15, 2009, 7:19 a.m.

This is great! Exactly what I was looking for...it actually simplified my code significantly!!!

Reply · Sept. 25, 2009, 1:19 a.m.

Quick question, though...When I switch between the activities, the tabs are no longer displayed since they are defined only in the Container class....do I need to define them in every activity then? If so, what's the point of having the container tabhost?

Thanks!

Reply · Sept. 25, 2009, 3:06 a.m.

Thanks for this, it is very simple and nice. I want the tabs to scroll horizontally, how can than be done programatically?

Reply · March 17, 2012, 7:02 p.m.