Monday, 1 April 2013

Tabs Demo - Vertical Tabs

Demo:


1)Activity:

package com.venky.tabsdemo3;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabsDemo3 extends TabActivity {
      /** Called when the activity is first created. */

      Button btnTab1, btnTab2, btnTab3;

      TabHost tabHost;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnTab1 = (Button) findViewById(R.id.tab1);
            btnTab2 = (Button) findViewById(R.id.tab2);
            btnTab3 = (Button) findViewById(R.id.tab3);

            tabHost = getTabHost();

            // Tab for Tab1
            TabSpec tab1 = tabHost.newTabSpec("Tab1");
            // setting Title and Icon for the Tab
            tab1.setIndicator("Tab1");
            Intent intent1 = new Intent(this, Tab1Activity.class);
            tab1.setContent(intent1);

            // Tab for Tab2
            TabSpec tab2 = tabHost.newTabSpec("Tab2");
            tab2.setIndicator("Tab2");
            Intent intent2 = new Intent(this, Tab2Activity.class);
            tab2.setContent(intent2);

            // Tab for Tab3
            TabSpec tab3 = tabHost.newTabSpec("Tab3");
            tab3.setIndicator("Tab3");
            Intent intent3 = new Intent(this, Tab3Activity.class);
            tab3.setContent(intent3);

            // Adding all TabSpec to TabHost
            tabHost.addTab(tab1); // Adding tab1 tab
            tabHost.addTab(tab2); // Adding tab2 tab
            tabHost.addTab(tab3); // Adding tab3 tab

      }

      public void tabHandler(View target) {
            btnTab1.setSelected(false);
            btnTab2.setSelected(false);
            btnTab3.setSelected(false);
            if (target.getId() == R.id.tab1) {
                  tabHost.setCurrentTab(0);
                  btnTab1.setSelected(true);
            } else if (target.getId() == R.id.tab2) {
                  tabHost.setCurrentTab(1);
                  btnTab2.setSelected(true);
            } else if (target.getId() == R.id.tab3) {
                  tabHost.setCurrentTab(2);
                  btnTab3.setSelected(true);
            }
      };

}



2)main.xml:
 
<?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="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <FrameLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.3" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="invisible" />

            <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/tab1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:onClick="tabHandler"
                        android:text="Tab1" />

                    <Button
                        android:id="@+id/tab2"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:onClick="tabHandler"
                        android:text="Tab2" />

                    <Button
                        android:id="@+id/tab3"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:onClick="tabHandler"
                        android:text="Tab3" />
                </LinearLayout>
            </ScrollView>
        </FrameLayout>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.7" />
    </LinearLayout>

</TabHost>


3)Download this Project Click Here
 

No comments:

Post a Comment