TabHost and TabWidget In Android:
Today we are going to see about Tabhost and TabWidget it's very easy concept.......
In following folder.
res/layout/activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc916"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="First" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#da8200"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Third" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
In MainActivity ,Inside oncreate() method.
1.Create TabHost object using getTabHost().
2.Create tabSpec object.
3.Set Indicator for the Spec.
4.Set Content for the Spec.
These fours steps to achieve tab in android.
//Tab
TabSpec firstTabSpec = tabHost.newTabSpec("First");
//Set title and icon for the tab
firstTabSpec .setIndicator("First",R.drawable.icon);
Intent firstIntent = new Intent(this,FirstActivity.class);
firstTabSpec.setContent(firstIntent);
//finally add to tabhost
tabhost.addTab(firstIntent);
Today we are going to see about Tabhost and TabWidget it's very easy concept.......
Step :1
First Create Project in android studio, Add TabHost in XML Activity Layout.In following folder.
res/layout/activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc916"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="First" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#da8200"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Third" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Step 2
In MainActivity ,Inside oncreate() method.
1.Create TabHost object using getTabHost().
2.Create tabSpec object.
3.Set Indicator for the Spec.
4.Set Content for the Spec.
These fours steps to achieve tab in android.
Example
TabHost tabhost = getTabHost();//Tab
TabSpec firstTabSpec = tabHost.newTabSpec("First");
//Set title and icon for the tab
firstTabSpec .setIndicator("First",R.drawable.icon);
Intent firstIntent = new Intent(this,FirstActivity.class);
firstTabSpec.setContent(firstIntent);
//finally add to tabhost
tabhost.addTab(firstIntent);
No comments:
Post a Comment