Translate To Your Own Langauge

Implementing Tab Example in android using TabActivity


Tab is the best way to display more than one page in a single page. For example, we have three .xml files and we want to show one file at a time but if user wants to see other files then he/she can see those by clicking on the tabs. The concept can be more cleared with the following example:



As shown in the above screenshot that we have three tabs in our example i.e
  • Home
  • About us
  • Contact us
We will implement Tab example by extending TabActivity class into our mainActivity class. Follow these steps:


  • Make new project and name it as "TabExapmle" and then type down the name of package.
  • Now make three .xml files in Tabexaple/res.
  1. firsttab.xml
  2. secondtab.xml
  3. thirdtab.xml
  • Now copy and paste the following code into these three files respectively:
firsttab.xml
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:textSize="25sp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="This is Home tab."
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="AT4U" />

</RelativeLayout>


secondtab.xml
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:textSize="25sp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="This is About us tab."
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="AT4U" />

</RelativeLayout>

thirdtab.xml
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:textSize="25sp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="This is Contact us tab."
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="AT4U" />

</RelativeLayout>


  • Now inside src folder, make three .java files
  1. first.java
  2. second.java
  3. third.java
  • Add the following code in these three files.
first.java

package com.example.tabexample;

import android.app.Activity;
import android.os.Bundle;

public class first extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              setContentView(R.layout.firsttab);
       }

}


 second.java
package com.example.tabexample;

import android.app.Activity;
import android.os.Bundle;

public class first extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              setContentView(R.layout.secondtab);
       }

}


 third.java
package com.example.tabexample;

import android.app.Activity;
import android.os.Bundle;

public class first extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              setContentView(R.layout.thirdtab);
       }

}





  • Now come to the mainActivity.java file and copy the following code in that file
MainActivity.java

package com.example.tabexample;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              //setContentView(R.layout.activity_main);

              TabHost tab = getTabHost();
              tab.setup();

              // For first tab
              TabHost.TabSpec spe = tab.newTabSpec("first");  //id
              spe.setIndicator("Home");                                     //text
              Intent it=new Intent(getApplicationContext(), first.class);
              spe.setContent(it);
              tab.addTab(spe);

              // For second tab
              spe = tab.newTabSpec("second"); // id
              spe.setIndicator("about us"); // text to shown on tab bar
              Intent it1=new Intent(getApplicationContext(), second.class);
              spe.setContent(it1);
              tab.addTab(spe);

              // For Third tab
              spe = tab.newTabSpec("third"); // id
              spe.setIndicator("Contact us"); // text to shown on tab bar
              Intent it2=new Intent(getApplicationContext(), third.class);
              spe.setContent(it2);
              tab.addTab(spe);
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

}


  • Your project is ready. Now run this project on Mobile or on emulator. If you don't know how to create emulator in ecllipse then click here

37 Responses to “Implementing Tab Example in android using TabActivity”

rimpi said...
16 January 2015 at 08:49

Outstanding tutorial... Very useful for me.


ganga pragya said...
2 September 2018 at 23:20

Really you have done great job,There are may person searching about that now they will find enough resources by your post
Block Chain Training in annanagar

Block Chain Training in pune

Block Chain Training in velachery


ganga pragya said...
3 September 2018 at 01:41 This comment has been removed by the author.

Unknown said...
13 September 2018 at 23:27

This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.


Data Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore


Unknown said...
19 September 2018 at 06:09 This comment has been removed by the author.

gowsalya said...
20 September 2018 at 22:03

I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
python training in velachery
python training institute in chennai


Unknown said...
3 October 2018 at 02:37

Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.
Blueprism training in btm

Blueprism online training


Unknown said...
6 October 2018 at 01:46

I think you have a long story to share and i am glad after long time finally you cam and shared your experience.


angularjs Training in chennai
angularjs-Training in pune

angularjs-Training in chennai

angularjs Training in chennai

angularjs-Training in tambaram


Unknown said...
8 October 2018 at 02:20

Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
Data Science Training in Chennai | Data Science training in anna nagar
Data Science training in chennai | Data science training in Bangalore
Data Science training in marathahalli | Data Science training in btm


janani said...
8 October 2018 at 23:24

That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
java training in jayanagar | java training in electronic city

java training in chennai | java training in USA


Unknown said...
9 March 2019 at 02:45

hank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me 
devops online training

aws online training

data science with python online training

data science online training

rpa online training


jvimala said...
8 April 2019 at 05:49

Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
Android Training in Chennai | Best Android Training in Chennai
Matlab Training in Chennai | Best Matlab Training in Chennai
Best AWS Training in Chennai | AWS Training in Chennai
Selenium Training in Chennai | Best Selenium Training in chennai
Devops Course Training in Chennai | Best Devops Training in Chennai


ramya said...
27 September 2019 at 02:52

It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
Data science Course Training in Chennai |Best Data Science Training Institute in Chennai
RPA Course Training in Chennai |Best RPA Training Institute in Chennai


Ahana said...
22 December 2021 at 07:43

I read your informative post. Surely, It would enhance the knowledge of android developers. Keep posting. Join us Best DevOps Training course in Noida for Global It Certificate Courses.


Anonymous said...
28 April 2022 at 21:36

mmorpg oyunlar
İNSTAGRAM TAKİPCİ SATIN AL
Tiktok Jeton Hilesi
Tiktok Jeton Hilesi
Saç Ekim Antalya
instagram takipçi satın al
Instagram takipci satın al
Metin2 Pvp Serverlar
İnstagram Takipci


Anonymous said...
3 June 2022 at 07:08

ataşehir vestel klima servisi
ümraniye bosch klima servisi
kartal arçelik klima servisi
ümraniye arçelik klima servisi
beykoz samsung klima servisi
üsküdar samsung klima servisi
beykoz mitsubishi klima servisi
üsküdar mitsubishi klima servisi
beykoz daikin klima servisi


nick said...
27 June 2022 at 03:12

Get your Tickets booked with our fastest and cheapest southwest flight 1535 Air Flights Booking service. Just search, compare and book or contact us for immediate booking assistance at +1(888) 707-1535


mrbahis said...
22 December 2022 at 10:42

Good content. You write beautiful things.
korsan taksi
hacklink
mrbahis
sportsbet
hacklink
vbet
taksi
sportsbet
vbet


canlı poker siteleri said...
26 December 2022 at 05:03

Success Write content success. Thanks.
betmatik
canlı slot siteleri
deneme bonusu
betpark
betturkey
kıbrıs bahis siteleri
kralbet


batuhan said...
31 July 2023 at 01:13

amasya
antakya
edirne
elazığ
kayseri

HH4DP


Büşra said...
12 August 2023 at 11:29

salt likit
salt likit
dr mood likit
big boss likit
dl likit
dark likit
X2N


baran said...
29 August 2023 at 13:26

https://saglamproxy.com
metin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
VXFZ


InfinityNinja9000 said...
30 September 2023 at 18:22

Bursa
Mersin
izmir
Rize
Antep
VDZFFQ


Hüdayi5 said...
15 October 2023 at 12:19

https://titandijital.com.tr/
elazığ parça eşya taşıma
malatya parça eşya taşıma
bilecik parça eşya taşıma
antalya parça eşya taşıma
UVHADS


HyperspaceFlare12AT said...
23 October 2023 at 02:32

sivas evden eve nakliyat
erzurum evden eve nakliyat
bitlis evden eve nakliyat
mardin evden eve nakliyat
rize evden eve nakliyat
8HZ4C


E70A0TimothyD3C95 said...
6 November 2023 at 07:31

5927F
Bitlis Evden Eve Nakliyat
Tekirdağ Parça Eşya Taşıma
Afyon Evden Eve Nakliyat
Mersin Evden Eve Nakliyat
Uşak Parça Eşya Taşıma


2245CAshtynCB1BE said...
7 November 2023 at 20:18

0A6C1
Tekirdağ Evden Eve Nakliyat
Kars Evden Eve Nakliyat
Tekirdağ Çatı Ustası
Ünye Evden Eve Nakliyat
Kırklareli Evden Eve Nakliyat


CEC1EItzelFEF0C said...
9 November 2023 at 01:54

0F097
Altındağ Boya Ustası
Çerkezköy Çatı Ustası
Batman Şehir İçi Nakliyat
Kütahya Lojistik
Muş Şehir İçi Nakliyat
Etimesgut Fayans Ustası
Gümüşhane Şehirler Arası Nakliyat
Diyarbakır Evden Eve Nakliyat
Niğde Şehir İçi Nakliyat


7C81EDarren5C207 said...
9 November 2023 at 05:43

D56CD
Tokat Şehirler Arası Nakliyat
Isparta Evden Eve Nakliyat
Batman Lojistik
Eskişehir Lojistik
Karabük Evden Eve Nakliyat
Ünye Halı Yıkama
Hakkari Parça Eşya Taşıma
Altındağ Parke Ustası
Çerkezköy Mutfak Dolabı


0A7B0NylahCA9C6 said...
9 November 2023 at 06:43

1F84E
Kastamonu Parça Eşya Taşıma
Denizli Şehirler Arası Nakliyat
Elazığ Parça Eşya Taşıma
Malatya Parça Eşya Taşıma
Kripto Para Nedir
Batıkent Fayans Ustası
Çerkezköy Mutfak Dolabı
Çorum Şehir İçi Nakliyat
Ardahan Parça Eşya Taşıma


AC334DakotaA6EAE said...
9 November 2023 at 12:07

9E100
Bolu Parça Eşya Taşıma
Ardahan Evden Eve Nakliyat
Manisa Şehir İçi Nakliyat
Kütahya Parça Eşya Taşıma
Uşak Şehirler Arası Nakliyat
Çorlu Lojistik
Antalya Evden Eve Nakliyat
Paribu Güvenilir mi
Nevşehir Lojistik


FF662Larry05D76 said...
9 November 2023 at 21:40

CB11B
Yozgat Şehirler Arası Nakliyat
Yalova Şehirler Arası Nakliyat
Çankırı Evden Eve Nakliyat
Nevşehir Evden Eve Nakliyat
İstanbul Evden Eve Nakliyat
Balıkesir Evden Eve Nakliyat
Osmaniye Şehirler Arası Nakliyat
Bingöl Lojistik
Kayseri Evden Eve Nakliyat


DE9CFKenzie411A0 said...
10 November 2023 at 05:46

C7321
Düzce Lojistik
Gümüşhane Şehirler Arası Nakliyat
Erzincan Lojistik
Antalya Evden Eve Nakliyat
Bingöl Evden Eve Nakliyat
Urfa Lojistik
Karabük Şehirler Arası Nakliyat
Amasya Parça Eşya Taşıma
Silivri Parke Ustası


CCBD7KailynF376F said...
21 December 2023 at 11:50

55154
en iyi görüntülü sohbet uygulamaları
sinop parasız sohbet siteleri
trabzon görüntülü sohbet yabancı
yabancı görüntülü sohbet uygulamaları
siirt yabancı canlı sohbet
bitlis kadınlarla ücretsiz sohbet
çankırı bedava sohbet odaları
muş canlı sohbet et
ücretsiz görüntülü sohbet


C8A84RoyAE99B said...
28 December 2023 at 13:37

8377B
görüntülü sohbet odaları
Ağrı Sesli Sohbet Siteler
Izmir Görüntülü Sohbet Canlı
canlı sohbet ücretsiz
sivas mobil sohbet siteleri
Karabük Görüntülü Sohbet
maraş parasız sohbet siteleri
eskişehir sesli mobil sohbet
Sivas Canlı Sohbet Sitesi


1B31BSelenaECFD1 said...
20 January 2024 at 18:14

6972F
Sonm Coin Hangi Borsada
Alyattes Coin Hangi Borsada
Bitcoin Nasıl Kazılır
Kripto Para Üretme
Kripto Para Çıkarma
Soundcloud Reposts Satın Al
Onlyfans Takipçi Satın Al
Coin Nasıl Üretilir
Ceek Coin Hangi Borsada


EEB1DCharles09A97 said...
20 January 2024 at 18:15

523C1
Görüntülü Sohbet
Kripto Para Kazanma Siteleri
Clubhouse Takipçi Hilesi
Kripto Para Çıkarma
Coin Oynama
Omlira Coin Hangi Borsada
Twitch İzlenme Satın Al
Snapchat Takipçi Satın Al
Bitcoin Mining Nasıl Yapılır


Post a Comment

Comment your feedback for the post. You can also ask your queries regarding android.

Blogger Widgets
All Rights Reserved Android Tutorials | Designed by: Deepak Anand