Skip to main content

Tab Layout in android


Tab Layout in android/Tab activity in android

Using tab layout, we can wrap multiple views in a single window and navigate through these views using tabs. Tab host, Tab widget and a frame layout are the important components used in the tab layout.
In this project, we are creating three tabs named Artist, Album and Songs, and while clicking on each of these tabs, different page (means activity) will be loaded.
First create a new project and add a tab host to your xml like this

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

Some rules that we must use while implementing tab activity is

 If the activity is of type TabActivity then the TabHost must have the id @android:id/tabhost.
·         The TabWidget must have the id @android:id/tabs.
·         The FrameLayout must have the id @android:id/tabcontent.

Then, create three activities. One for Songs, one for Albums and other for Artist. Then load textview dynamically for each activity. (For easiness, otherwise create three xml for each of these activities)
  TextView textview = new TextView(this);

        textview.setText("This is the Artists tab");

        setContentView(textview);

Now create a main activity and extends the class with TabActivty instead of Activity.
public class HelloTabWidget extends TabActivity {
Then do the following codes
Resources res = getResources(); // Resource object to get Drawables

    TabHost tabHost = getTabHost();  // The activity TabHost

    TabHost.TabSpec spec;  // Resusable TabSpec for each tab

    Intent intent;  // Reusable Intent for each tab



    // Create an Intent to launch an Activity for the tab (to be reused)

    intent = new Intent().setClass(this, ArtistsActivity.class);



    // Initialize a TabSpec for each tab and add it to the TabHost

    spec = tabHost.newTabSpec("artists").setIndicator("Artists",

                      res.getDrawable(R.drawable.ic_tab_artists))

                  .setContent(intent);

    tabHost.addTab(spec);

A reference to the TabHost is first captured with getTabHost(). Then, for each tab, a TabHost.TabSpec is created to define the tab properties. The newTabSpec(String) method creates a new TabHost.TabSpec identified by the given string tag. For each TabHost.TabSpec, setIndicator(CharSequence, Drawable) is called to set the text and icon for the tab, and setContent(Intent) is called to specify the Intent to open the appropriate Activity. EachTabHost.TabSpec is then added to the TabHost by calling addTab(TabHost.TabSpec).
At the very end, setCurrentTab(int) opens the tab to be displayed by default, specified by the index position of the tab.
And run the application

main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
   
android:id="@android:id/tabhost"
   
android:layout_width="fill_parent"
   
android:layout_height="fill_parent">
   
<LinearLayout
       
android:orientation="vertical"
       
android:layout_width="fill_parent"
       
android:layout_height="fill_parent"
       
android:padding="5dp">
       
<TabWidget
           
android:id="@android:id/tabs"
           
android:layout_width="fill_parent"
           
android:layout_height="wrap_content" />
       
<FrameLayout
           
android:id="@android:id/tabcontent"
           
android:layout_width="fill_parent"
           
android:layout_height="fill_parent"
           
android:padding="5dp" />
   
</LinearLayout>
</TabHost>

ArtistsActivity.java



public class ArtistsActivity extends Activity {


    public void onCreate(Bundle savedInstanceState) {



  super.onCreate(savedInstanceState);


        TextView textview = new TextView(this);


        textview.setText("This is the Artists tab");


        setContentView(textview);


    }


}


AlbumActivity,java

public class AlbumsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      TextView textview = new TextView(this);
        textview.setText("This is the Artists tab");
        setContentView(textview);
    }
}

SongsActivity.java

public class SongssActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textview = new TextView(this);
        textview.setText("This is the Artists tab");
        setContentView(textview);
    }
}
 HelloTabWidget

public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost

    spec = tabHost.newTabSpec("artists").setIndicator("Artists",

                      res.getDrawable(R.drawable.ic_tab_artists))

                  .setContent(intent);

    tabHost.addTab(spec);

    // Do the same for the other tabs

    intent = new Intent(this, AlbumsActivity.class);

    spec = tabHost.newTabSpec("albums").setIndicator("Albums",

                      res.getDrawable(R.drawable.ic_tab_albums))

                  .setContent(intent);

    tabHost.addTab(spec);



    intent = new Intent(this, SongsActivity.class);

    spec = tabHost.newTabSpec("songs").setIndicator("Songs",

                      res.getDrawable(R.drawable.ic_tab_songs))

                  .setContent(intent);

    tabHost.addTab(spec);



    tabHost.setCurrentTab(2);

}

(note:::Add all the activities in the manifest file….)

RESULT


See More

Comments

  1. this tutorial meant very finew to me,but do u know how to adjust the space between tabs

    ReplyDelete
  2. hi,sorry for the delayed reply...
    use the following line of code to change the height and width of your tab
    tabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(width,height));
    width and height indicates the desired height and width of each tabs. By changing the appropriate indexes of getChildAt()method, you can adjust the size of each tab...

    ReplyDelete

Post a Comment

Popular posts from this blog

Capture image without surface view as background service in android

Capture image without user knowledge in android/Capture image as background service in android/Take photo without preview in android/ Camera without preview in android  package com.example.picture; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.AudioManager; import android.os.IBinder; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraService extends Service {       //Camera variables       //a surface holder       private SurfaceHolder sHolder ;        //a variable to control the camera       private Camera mCamera ;       //the camera parameters       private Parameters parameters ;       /** Called when the activity is first created. */     @Override     p

Custom Spinner in Android

Custom Spinner in Android/Spinner using custom adapter/spinner with icon and text  In this tutorial, i'll show you to customizing spinner in different methods.  To change the normal display of spinner view, add background image using the code " android:background="@drawable/myspinner_background" in the xml. You can also make it customizable by setting up an xml file in drawable folder Custom display with icon in the spinner   main.xml <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"     android:orientation = "vertical"     android:layout_width = "fill_parent"     android:layout_height = "fill_parent"     >     < Spinner     android:drawSelectorOnTop = "true"     android:id = "@+id/spinner1"     android:layout_width = "match_parent"     android:layout_height = "wrap_content&

Coroutine- A complete tutorial

 What are coroutines? To know about coroutine, first you need to know about asynchronous programming, thread and multithreading concept. What is a thread? Thread describes in which context the function or sequence of instructions should be executed. So, every block of code or functions runs in a thread, right? Also, you can load multiple threads to perform different block of codes  How to start a thread? Thread thread = new Thread(){     public void run(){       System.out.println("This is a thread");     }   }     thread.start(); So when coming to android, before learning about coroutines we need to discuss some scenarios. Normally how an app works when user launch an application. When user launchers the application, a main thread is created. This thread is intended to do small operations like button clicks, UI interaction , small mathematical operations. We cant perform long running operation like file download, database queries, network operations and image