Monday 3 September 2012

Android TabLayout example


In this example we have three tab as follows. you can move in the any tab at by clicking on the Tab. as follows

  1. First we design the XML layout as follows

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@android:id/tabhost" android:layout_width="match_parent"
     android:layout_height="match_parent">
     <LinearLayout android:id="@+id/linearLayout1"
      android:layout_width="match_parent" android:layout_height="match_parent"
      android:orientation="vertical">
      <TabWidget android:layout_width="match_parent"
       android:layout_height="wrap_content" 
       android:id="@android:id/tabs"></TabWidget>
      <FrameLayout android:layout_width="match_parent"
       android:layout_height="match_parent" android:id="@android:id/tabcontent">
       
      </FrameLayout>
     </LinearLayout>
    </TabHost>
    
    


  2.  Create the Activtity file for diffrent Tab Activities we have create three diffrent Activity : ActivityA, ActivityB, ActivityC as follows


  3. package com.container;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class ActivityA extends Activity {
    
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         TextView tv=new TextView(this);
         tv.setText("I am in Tab A..");
         setContentView(tv);
         // TODO Auto-generated method stub
     }
    
    }
    
    package com.container;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class ActivityB extends Activity {
    
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         TextView tv=new TextView(this);
         tv.setText("I am in Tab B..");
         setContentView(tv);
         // TODO Auto-generated method stub
     }
    
    }
    
    package com.container;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class ActivityC extends Activity {
    
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         TextView tv=new TextView(this);
         tv.setText("I am in Tab C..");
         setContentView(tv);
         // TODO Auto-generated method stub
     }
    
    }
    
  4. Than we Create Our main Activity as follows code..


  5. package com.container;
    
    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.TabHost;
    
    public class TabViewDemoActivity extends TabActivity {
        /** Called when the activity is first created. */
     Resources res;
     TabHost tabHost;
     TabHost.TabSpec spec;
     Intent intent;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            res=getResources();
            tabHost=getTabHost();
            
            /**Create Tab A**/
            intent=new Intent().setClass(this,ActivityA.class);
            spec=tabHost.newTabSpec("A")
              .setIndicator("A",res.getDrawable(R.drawable.ic_btn_speak_now))
              .setContent(intent);
            tabHost.addTab(spec);
            
            /**Create Tab B**/
            intent=new Intent().setClass(this,ActivityB.class);
            spec=tabHost.newTabSpec("B")
              .setIndicator("B",res.getDrawable(R.drawable.ic_btn_speak_now))
              .setContent(intent);
            tabHost.addTab(spec);
            
            /**Create Tab C**/
            intent=new Intent().setClass(this,ActivityC.class);
            spec=tabHost.newTabSpec("C")
              .setIndicator("C",res.getDrawable(R.drawable.ic_btn_speak_now))
              .setContent(intent);
            tabHost.addTab(spec);
        }
    }


that it, Run your app you can move on any Tab By clicking on that as follows

6 comments: