About
Home > Tutorials Home > Android > TabHost Application In Android

TabHost Application In Android

Published on October 17, 2019 8:10:04 PM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "TabHost Example in Android".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
    android:orientation="vertical" >

	<TabHost
		android1:id="@+id/tabhost"
		android1:layout_width="fill_parent"
		android1:layout_height="fill_parent">

		<LinearLayout
			android1:layout_width="fill_parent"
			android1:layout_height="fill_parent"
			android1:orientation="vertical">

			<TabWidget
				android1:id="@android:id/tabs"
				android1:layout_width="fill_parent"
				android1:layout_height="wrap_content">
			</TabWidget>

			<FrameLayout
				android1:id="@android:id/tabcontent"
				android1:layout_width="match_parent"
				android1:layout_height="match_parent">

				<LinearLayout
					android1:id="@+id/tab1"
					android1:layout_width="fill_parent"
					android1:layout_height="fill_parent">

					<AnalogClock
						android1:id="@+id/analogClock1"
						android1:layout_width="fill_parent"
						android1:layout_height="fill_parent"
						android1:gravity="center"/>
			</LinearLayout>
			
			<LinearLayout
				android1:id="@+id/tab2"
				android1:layout_width="fill_parent"
				android1:layout_height="fill_parent">

				<CalendarView
					android1:layout_width="fill_parent"
					android1:layout_height="fill_parent"/>
			</LinearLayout>
			<LinearLayout
				android1:id="@+id/tab3"
				android1:layout_width="fill_parent"
				android1:layout_height="fill_parent">
	
				<Button
					android1:layout_width="fill_parent"
					android1:layout_height="fill_parent"
					android1:text="Button"/>
			</LinearLayout>	
		</FrameLayout>
	</LinearLayout>
</TabHost>
</LinearLayout>

Step 2 : Copy the Following Code into your .Java File.

package com.example.tabhostexample1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TabHost;

public class MainActivity extends Activity {

	private TabHost tbh;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tbh=(TabHost)findViewById(R.id.tabhost);
		tbh.setup();

		TabHost.TabSpec spec;

		spec=tbh.newTabSpec("Clock");
		spec.setIndicator("Tab1");
		spec.setContent(R.id.tab1);
		tbh.addTab(spec);

		spec=tbh.newTabSpec("Calendar");
		spec.setIndicator("Tab2");
		spec.setContent(R.id.tab2);
		tbh.addTab(spec);

		spec=tbh.newTabSpec("Button");
		spec.setIndicator("Tab3");
		spec.setContent(R.id.tab3);
		tbh.addTab(spec);

		tbh.setCurrentTab(0);
	}
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile