I need sample code to create TabHost in android. can anyone help me.
Important Methods of TabHostaddTab(TabSpec tabSpec): This method is used to add the new tab onto a tab widget. Whenever a new tab is being specified using TabSpec class, one needs to add that tab in our TabHost. // initiate TabHost TabHost tabhost = findViewById(R. id.
Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page.
In Android, TabHost is a Container for tabbed window view. This object holds two children one is set of tab labels that the user clicks to select a specific tab and other is a FrameLayout object that displays the content of that page.
The Android Developer site has an excellent fully worked code sample for creating tabs in Android using the TabWidget
and TabHost
.
Check out Hello, TabWidget.
I had done tabhost related code with respect to displaying scores
TabHost host = getTabHost();
host.setup ();
TabSpec allScoresTab = host.newTabSpec("allTab");
allScoresTab.setIndicator(getResources().getString(R.string.all_scores), getResources().getDrawable(android.R.drawable.star_on));
allScoresTab.setContent(R.id.ScrollViewAllScores);
host.addTab(allScoresTab);
TabSpec friendScoresTab = host.newTabSpec("friendsTab");
friendScoresTab.setIndicator(getResources().getString(R.string.friends_scores), getResources().getDrawable(android.R.drawable.star_on));
friendScoresTab.setContent(R.id.ScrollViewFriendScores);
host.addTab(friendScoresTab);
host.setCurrentTabByTag("allTab");
My xml contains :
<?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"
android:background="@drawable/bkgrnd">
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ImageView_Header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/quizicon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:text="@string/scores"
android:textSize="@dimen/screen_title_size"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:layout_width="wrap_content"
android:layout_gravity="fill_horizontal|center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:shadowColor="@android:color/white"
android:textColor="@color/title_color">
</TextView>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ImageView_Header2"
android:layout_height="wrap_content"
android:src="@drawable/quizicon"
android:layout_gravity="right|center_vertical"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
</ImageView>
</RelativeLayout>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="@+id/ScrollViewAllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
<TableLayout android:id="@+id/TableLayout_AllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" />
</ScrollView>
- <ScrollView android:id="@+id/ScrollViewFriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
<TableLayout android:id="@+id/TableLayout_FriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" />
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With