Start different activities in separate tabs of a TabHost

1 vote · 0 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>

Be the first to comment

Sign in with OpenID