Android TabHost provides a nice way to present multiple thing on a Single Screen. These things are presented by Tabs.
In order to use tabs we need to set 2 things to Tab
1: Tab Indicator : Text to show on Tab, done by setIndicator("Tab Name");
2: TAB Content: The activity that will be opened when user selects/clicks particular tab, done by setContent(activityObject);
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
public class MainActivity extends TabActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,Tab2Activity.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,Tab3Activity.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
Tab1Activity.java
public class Tab1Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab1 Activity");
setContentView(tv);
}
}
Tab2Activity.java
public class Tab2Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
Tab3Activity.java
public class Tab3Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab3 Activity");
setContentView(tv);
}
}
In order to use tabs we need to set 2 things to Tab
1: Tab Indicator : Text to show on Tab, done by setIndicator("Tab Name");
2: TAB Content: The activity that will be opened when user selects/clicks particular tab, done by setContent(activityObject);
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
TabActivity.java
public class MainActivity extends TabActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,Tab2Activity.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,Tab3Activity.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
Tab1Activity.java
public class Tab1Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab1 Activity");
setContentView(tv);
}
}
Tab2Activity.java
public class Tab2Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
Tab3Activity.java
public class Tab3Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab3 Activity");
setContentView(tv);
}
}
New Advance Topics: Android LiveWallpaer Tutorial
Android ImageSwitcher Android TextSwitcher Android ViewFlipper
Android Gesture Detector Handling/Detecting Swipe Events Gradient Drawable
Detecting Missed Calls Hide Title Bar GridView Animation
Android AlarmManager Android BootReceiver Vibrate Phone In a Desirable Pattern
Developing for Different Screen Sizes Showing Toast for Longer Time Publishing your App
How to publish Android App on Google Play
Android TextWatcher Android ExpandableListView
Beginning With Android
Android : Introduction(What is Android) Configuring Eclipse for Android Development
Creating Your First Android Project Understanding Android Manifest File of your android app
Advance Android Topics Customizing Android Views
Working With Layouts Working With Views
Understanding Layouts in Android Using Buttons and EditText in Android
Working with Linear Layout (With Example) Using CheckBoxes in Android
Nested Linear Layout (With Example) Using AutoCompleteTextView in Android Grid View
Relative Layout In Android ListView
Table Layout Android ProgressBar
Frame Layout(With Example) Customizing ProgressBar
Absolute Layout Customizing Radio Buttons
Grid Layout Customizing Checkboxes In Android
Android Advance Views
Android Spinner Android GalleryView
Android TabWidget Android ExpandableListView
Android Components Dialogs In Android
Activity In Android Working With Alert Dialog
Activity Life Cycle Adding Radio Buttons In Dialog
Starting Activity For Result Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android Creating Customized Dialogs in Android
Returning Result from Activity Creating Dialog To Collect User Input
Android : Service DatePicker and TimePickerDialog
BroadcastReceiver Using TimePickerDialog and DatePickerDialog In android
Menus In Android ListView:
Creating Option Menu Populating ListView With DataBase
Creating Context Menu In Android Populating ListView with ArrayList
ListView with Custom Adapter
Toast Working With SMS
Customizing Toast In Android How to Send SMS in Android
Customizing the Display Time of Toast How To Receive SMS
Customizing Toast At Runtime Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time
TelephonyManager Storage: Storing Data In Android
Using Telephony Manager In Android SharedPreferences In Android
Reading and Writing files to Internal Stoarage
Working With Incoming Calls DataBase : Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android Working With Database in Android
How to Forward an Incoming Call In Android Creating Table In Android
CALL States In Android Inserting, Deleting and Updating Records In Table in Android
Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
How to Hide Title Bar In Android
How to show an Activity in Landscape or Portrait Mode only.
How to Set an Image as Wallpaper.
plz tel me hw to add buttons in the tabs
ReplyDeleteInstead of setting the TextView to print the "This is Tab* Activity" remove it and replace the setContetView(tv) to setContentView(R.layout.tab1);
Deletehi, how do you change the colour of the horizontal tabs to white ?
ReplyDeleteand how do i make it scrollable, if i were to add in many more tabs. Appreciate your kind help!
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteput the code in AndroidManifest.xml
ReplyDeleteNice and simple!
ReplyDeleteThanks!
Best Regards..
Thanks but i have got a questions: i wanna sets to tab icons , how to sets?
ReplyDeleteYou need to mention the drawable resource which you want to set as icon.
DeleteFinally an example that works. thanks a lot!
ReplyDeleteHi. I just want to ask what do you mean by "put the code in AndroidManifest.xml"? which code? I tried it and it's not working. Please Help :)
ReplyDeleteI have not mentioned it anywhere "put the code in AndroidManifest.xml".
Deletehey tell me whether tabactivity.java is same as main activity?
ReplyDeleteTabActivity is similar toActivity.
DeleteHi :) Well explained.
ReplyDeleteI still have a question left: Eclipse warns several times: "The type TabActivity is deprecated". What can i do to make it work nervertheless? I tried to run the App, but it failed.
it was nice and simple tutorial for beginners its a great work .
ReplyDeletethanks a lot
when i am launching this app its showing unfortunately stopped
ReplyDeleteyou have to all activities in the manifist. ie MainActivity , Tab1, Tab2,Tab3
DeleteAdd all Activities in manifest.make sure you have added these activities in manifest MainActivity,Tab1,TAb2 and Tab3
DeleteI can't solve it, is there anybody has the source I can try? Thank you
ReplyDeletesir how we add data connectivity in tab host???
ReplyDeletehow we can do database connectivity in hosttab
ReplyDeleteHI it's very good example but how do i add buttons to the tab activity or creating a layout for the tab activity?
ReplyDeletehow can i change background color of tabhost?
ReplyDeletecan i take any other layout in place of frame layout?
ReplyDeleteÓtimo! Esse exemplo funcionou... =D
ReplyDeleteEu adicionei o código no Manifest... Mas não tentei sem colocar.
É possível alternar as tabs arrastando o dedo na tela? Como posso fazer isso?
Hello,
ReplyDeletei am a beginer to develop Android.
i want to integrate SessionManager into my app (with tabhost).
e.g. Tab2Activity is my Login activity. and Tab1Activity is the acitvity that fetch infos of user from database.
how can i see the user-information in Tab1Activity?
can someone help me please?
thenks.
Hello,
ReplyDeletei am a beginer to develop Android.
i want to integrate SessionManager into my app (with tabhost).
e.g. Tab2Activity is my Login activity. and Tab1Activity is the acitvity that fetch infos of user from database.
how can i see the user-information in Tab1Activity?
can someone help me please?
thenks.
how can i change the textcolor of a tab when pressed?
ReplyDeleteTabActivity is dippricated 13 and above versions right how can I create Tabs without using the TabActivity
ReplyDeleteHow can i add a swipe for changing tabs
ReplyDeleteSuper easy and easy to understand! Tnks!
ReplyDeleteTabActivity is now deprecated.
ReplyDeletethanks
ReplyDeleteHi Kamlesh! I'm wondering if you have an android tutorial for listview within tabwidget? and listview shows data from remote database.
ReplyDeleteHi,
ReplyDeleteYou have created TabActivity.java... The class which contains the onCreate() is MainActivity and it extends TabActivity.
Are you sure it will be this way?
Coz TabActivity is shown to be deprecated.
Hi,
ReplyDeleteI have tried your example, and when I add the tabs to the tabhost my app crushes.
i already try, no errors but whe i run the application unfortunatelly has stopped
ReplyDeleteplease help
thanks
you should add the three class which extends activity to the manifist without intent_filter like this>>>>
Delete/** Add the tabs to the TabHost to display. */
ReplyDeletetabHost.addTab(tab1); ===> Here its show null pointer exception, any other way to solve it
tabHost.addTab(tab2);
tabHost.addTab(tab3);
Nice tutorial.... thanx man..
ReplyDeleteHow to make tabs swipable can you please help with that also...
Thank you so much ... works well , if anyone getting errors try to add all activities to manifest file..
ReplyDeleteEclipse warns several times: "The type TabActivity is deprecated". What can i do? Please help me
ReplyDeletethanks
ReplyDeletei want to click “this is tab 1 screen “, “this is tab 2 screen” and “this is tab 3 screen” then it open new fragment…. then what i do ??i take different different fragment for tab 1,tab 2, or tab 3…….??? or common fragment for all…kindly suggest me i confused……??? if take common then how we use it …..?????
ReplyDeleteMost mentors will agree you to convey them through telecommunicate or the telecom at set present. java
ReplyDeleteAivivu - đại lý chuyên vé máy bay trong nước và quốc tế
ReplyDeleteVe may bay di My
vé máy bay từ mỹ về việt nam 2021
đăng ký bay từ canada về Việt Nam
é bay từ nhật về việt nam
Có chuyến bay từ Hàn Quốc về Việt Nam không
Vé máy bay từ Đài Loan về VN
khách sạn cách ly hà nội
vé máy bay chuyên gia nước ngoài sang Việt Nam
Hi,
ReplyDeleteThanks for sharing the information with us it was very informative. https://hangup.in