Divide a long activiy into tabbed areas using TabHost

3 votes · 2 comments

Divide up large activities with tabs. The full source of this example and others is available here: https://github.com/novoda/android/

raw ·
copy
· download
/*** * src/com.novoda/SelfContainedTabHost.java */ public class SelfContainedTabHost extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabhost_container); TabHost tabs = (TabHost)this.findViewById(R.id.tabhost); tabs.setup(); TabHost.TabSpec one = tabs.newTabSpec("one"); one.setContent(R.id.tab1content); one.setIndicator("TAB 1"); tabs.addTab(one); TabHost.TabSpec two = tabs.newTabSpec("two"); two.setContent(R.id.tab2content); two.setIndicator("TAB 2"); tabs.addTab(two); tabs.setCurrentTab(0); } } /*** * res/layout/tabhost_container.xml */ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="105px"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="65px"> <LinearLayout android:id="@+id/tab1content" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00008B"> <!-- Put Tab 1 Views in here --> </LinearLayout> <LinearLayout android:id="@+id/tab2content" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFF00"> <!-- Put Tab 2 Views in here --> </LinearLayout> </FrameLayout> </TabHost> </LinearLayout> /*** * 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=".SelfContainedTabHost" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Add a comment

2 Comments

Great ! it's what i'm looking for ! Thanks

Reply · Aug. 5, 2009, 2:49 a.m.

Muito bom! Me ajudou muito!

Reply · July 30, 2011, 1:17 a.m.