tabs | Android

Tabs.java

package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class Tabs extends Activity implements OnClickListener{
    Button bStart, bStop, addTab;
    TextView showResults;
    long  start,stop;

    TabHost th;

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

        bStart=(Button) findViewById(R.id.bStartWatch);
        bStop=(Button) findViewById(R.id.bStopWatch);
        addTab=(Button) findViewById(R.id.bAddTab);
        showResults=(TextView)findViewById(R.id.tvShowResults);

        addTab.setOnClickListener(this);
        bStart.setOnClickListener(this);
        bStop.setOnClickListener(this);


        th= (TabHost)findViewById(R.id.tabhost);
        th.setup();

        TabSpec specs=th.newTabSpec("tag1");

        specs.setContent(R.id.tab1);
        specs.setIndicator("StopWatch");//Frist Tab
        th.addTab(specs);

        //Second Tab        specs=th.newTabSpec("tag2");

        specs.setContent(R.id.tab2);
        specs.setIndicator("Tab 2");//second Tab
        th.addTab(specs);

        //Adda tab        specs=th.newTabSpec("tag3");

        specs.setContent(R.id.bAddTab);
        specs.setIndicator("Button Add Tab");//Frist Tab
        th.addTab(specs);
        start=0;




    }

    @Override    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId()){
            case R.id.bAddTab:
                TabSpec ourSpec= th.newTabSpec("tag1");
                ourSpec.setContent(new TabHost.TabContentFactory() {

                    @Override                    public View createTabContent(String arg0) {
                        // TODO Auto-generated method stub                        TextView text=new TextView(Tabs.this);
                        text.setText("You have created new tab!!");



                        return (text);

                    }

                });
                ourSpec.setIndicator("New ");
                th.addTab(ourSpec);


                break;
            case R.id.bStartWatch:

                start= System.currentTimeMillis();
                break;
            case R.id.bStopWatch:
                stop=System.currentTimeMillis();

                if(start!=0){
                    long result=stop-start;
                    int millis=(int) result;
                    int seconds=(int) result/1000;
                    int minutes=(int) seconds/60;

                    millis%=100;
                    seconds%=60;

                    showResults.setText(String.format("%d:%02d:%02d",minutes,seconds,millis));
                }

                break;

        }


    }


}



2.tabs.xml

xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width= "match_parent"    android:layout_height= "match_parent"    android:orientation= "vertical" >

    <TabHost        android:id="@+id/tabhost"        android:layout_width="match_parent"        android:layout_height="match_parent" >

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

            <TabWidget                android:id="@android:id/tabs"                android:layout_width="423dp"                android:layout_height="79dp"></TabWidget>

            <FrameLayout                android:id="@android:id/tabcontent"                android:layout_width="match_parent"                android:layout_height="match_parent"                >

                <LinearLayout                    android:id="@+id/tab1"                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:orientation="horizontal"
                    >

                    <Button                        android:id="@+id/bStartWatch"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="RESET" />

                    <Button                        android:id="@+id/bStopWatch"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="Stop" />

                    <TextView                        android:id="@+id/tvShowResults"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="TextView" />

                </LinearLayout>

                <LinearLayout                    android:id="@+id/tab2"                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:orientation="horizontal" >
                    <TextView                        android:id="@+id/textView1"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="TextView 2" />
                </LinearLayout>

                <LinearLayout                    android:id="@+id/tab3"                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:orientation="horizontal" >
                    <Button                        android:id="@+id/bAddTab"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text=" B Add Tab" />
                </LinearLayout>

            </FrameLayout>
        </LinearLayout>
    </TabHost >

</LinearLayout>




Reactions:

0 comments:

Post a Comment