Thursday, July 25, 2013

Android TabWidget Example

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);

main.xml


Android TabHost

<?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);
        }
}




Android TabHost






 

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.






49 comments:

  1. plz tel me hw to add buttons in the tabs

    ReplyDelete
    Replies
    1. Instead of setting the TextView to print the "This is Tab* Activity" remove it and replace the setContetView(tv) to setContentView(R.layout.tab1);

      Delete
  2. hi, how do you change the colour of the horizontal tabs to white ?

    ReplyDelete
  3. and how do i make it scrollable, if i were to add in many more tabs. Appreciate your kind help!

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. put the code in AndroidManifest.xml








    ReplyDelete
  7. Nice and simple!

    Thanks!

    Best Regards..

    ReplyDelete
  8. Thanks but i have got a questions: i wanna sets to tab icons , how to sets?

    ReplyDelete
    Replies
    1. You need to mention the drawable resource which you want to set as icon.

      Delete
  9. Finally an example that works. thanks a lot!

    ReplyDelete
  10. Hi. 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 :)

    ReplyDelete
    Replies
    1. I have not mentioned it anywhere "put the code in AndroidManifest.xml".

      Delete
  11. hey tell me whether tabactivity.java is same as main activity?

    ReplyDelete
  12. Hi :) Well explained.

    I 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.

    ReplyDelete
  13. it was nice and simple tutorial for beginners its a great work .
    thanks a lot

    ReplyDelete
  14. when i am launching this app its showing unfortunately stopped

    ReplyDelete
    Replies
    1. you have to all activities in the manifist. ie MainActivity , Tab1, Tab2,Tab3

      Delete
    2. Add all Activities in manifest.make sure you have added these activities in manifest MainActivity,Tab1,TAb2 and Tab3

      Delete
  15. I can't solve it, is there anybody has the source I can try? Thank you

    ReplyDelete
  16. sir how we add data connectivity in tab host???

    ReplyDelete
  17. how we can do database connectivity in hosttab

    ReplyDelete
  18. HI it's very good example but how do i add buttons to the tab activity or creating a layout for the tab activity?

    ReplyDelete
  19. how can i change background color of tabhost?

    ReplyDelete
  20. can i take any other layout in place of frame layout?

    ReplyDelete
  21. Ótimo! Esse exemplo funcionou... =D
    Eu 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?

    ReplyDelete
  22. Hello,
    i 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.

    ReplyDelete
  23. Hello,
    i 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.

    ReplyDelete
  24. how can i change the textcolor of a tab when pressed?

    ReplyDelete
  25. TabActivity is dippricated 13 and above versions right how can I create Tabs without using the TabActivity

    ReplyDelete
  26. How can i add a swipe for changing tabs

    ReplyDelete
  27. Super easy and easy to understand! Tnks!

    ReplyDelete
  28. TabActivity is now deprecated.

    ReplyDelete
  29. Hi Kamlesh! I'm wondering if you have an android tutorial for listview within tabwidget? and listview shows data from remote database.

    ReplyDelete
  30. Hi,
    You 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.

    ReplyDelete
  31. Hi,
    I have tried your example, and when I add the tabs to the tabhost my app crushes.

    ReplyDelete
  32. i already try, no errors but whe i run the application unfortunatelly has stopped
    please help

    thanks

    ReplyDelete
    Replies
    1. you should add the three class which extends activity to the manifist without intent_filter like this>>>>


      Delete
  33. /** Add the tabs to the TabHost to display. */
    tabHost.addTab(tab1); ===> Here its show null pointer exception, any other way to solve it
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);

    ReplyDelete
  34. Nice tutorial.... thanx man..
    How to make tabs swipable can you please help with that also...

    ReplyDelete
  35. Thank you so much ... works well , if anyone getting errors try to add all activities to manifest file..

    ReplyDelete
  36. Eclipse warns several times: "The type TabActivity is deprecated". What can i do? Please help me

    ReplyDelete
  37. i 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 …..?????

    ReplyDelete
  38. Most mentors will agree you to convey them through telecommunicate or the telecom at set present. java

    ReplyDelete
  39. Hi,
    Thanks for sharing the information with us it was very informative. https://hangup.in

    ReplyDelete