0% found this document useful (0 votes)
76 views8 pages

Mad Assignment 1

The document describes the code for a stopwatch Android application. It includes the XML layout file for the user interface, which contains text and buttons to display the timer, start, pause and reset. It also includes the Java code for the main activity, which handles setting up the UI elements, starting and updating the timer on a handler, and responding to button clicks to start, pause and reset the timer. The timer displays the minutes, seconds and milliseconds separated by colons.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views8 pages

Mad Assignment 1

The document describes the code for a stopwatch Android application. It includes the XML layout file for the user interface, which contains text and buttons to display the timer, start, pause and reset. It also includes the Java code for the main activity, which handles setting up the UI elements, starting and updating the timer on a handler, and responding to button clicks to start, pause and reset the timer. The timer displays the minutes, seconds and milliseconds separated by colons.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of computer science & Info-Technology (CS & IT)

Assignment no:1

BSSE-6th-A

Course: MAD

M bilal Amjad

70061192
TOPIC: STOP WATCH:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/an
droid"  

    xmlns:app="http://schemas.android.com/apk/res-auto"  

    xmlns:tools="http://schemas.android.com/tools"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent"  

    android:background="@color/colorPrimary"  

    tools:context="in.amitsin6h.stopwatch.MainActivity">  

  

        <RelativeLayout  

            android:layout_width="fill_parent"  

            android:layout_height="fill_parent"  

            android:layout_marginLeft="10dp"  

            android:layout_marginRight="10dp"  

            android:paddingBottom="90dp">  

  

  

           <TextView  

               android:text="00:00:00"  

               android:layout_width="wrap_content"  

               android:layout_height="wrap_content"  
               android:id="@+id/tvTimer"  

               android:textSize="50dp"  

               android:textStyle="bold"  

               android:textColor="#ffffff"  

               android:layout_marginTop="120dp"  

               android:paddingBottom="50dp"  

               android:layout_alignParentTop="true"  

               android:layout_centerHorizontal="true" />  

  

           <Button  

               android:text="Start"  

               android:background="#ffffff"  

               android:layout_width="wrap_content"  

               android:layout_height="wrap_content"  

               android:layout_below="@+id/tvTimer"  

               android:layout_alignParentLeft="true"  

               android:layout_alignParentStart="true"  

               android:layout_marginTop="41dp"  

               android:id="@+id/btStart" />  

  

           <Button  

               android:text="Pause"  

               android:background="#ffffff"  

               android:layout_width="wrap_content"  

               android:layout_height="wrap_content"  

               android:id="@+id/btPause"  

               android:layout_alignBaseline="@+id/btStart"  

               android:layout_alignBottom="@+id/btStart"  

               android:layout_centerHorizontal="true" />  
  

           <Button  

               android:text="Reset"  

               android:background="#ffffff"  

               android:layout_width="wrap_content"  

               android:layout_height="wrap_content"  

               android:layout_alignTop="@+id/btPause"  

               android:layout_alignParentRight="true"  

               android:layout_alignParentEnd="true"  

               android:id="@+id/btReset" />  

  

        </RelativeLayout>  

  

  

</android.support.constraint.ConstraintLayout>  

MainActivity.java
package in.amitsin6h.stopwatch;  

  

import android.os.Handler;  

import android.os.SystemClock;  

import android.support.v7.app.AppCompatActivity;  

import android.os.Bundle;  

import android.view.View;  

import android.widget.Button;  

import android.widget.TextView;  

  

public class MainActivity extends AppCompatActivity {  

  

    TextView timer ;  
    Button start, pause, reset;  

    long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;  

    Handler handler;  

    int Seconds, Minutes, MilliSeconds ;  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

  

        timer = (TextView)findViewById(R.id.tvTimer);  

        start = (Button)findViewById(R.id.btStart);  

        pause = (Button)findViewById(R.id.btPause);  

        reset = (Button)findViewById(R.id.btReset);  

  

        handler = new Handler() ;  

  

        start.setOnClickListener(new View.OnClickListener() {  

            @Override  

            public void onClick(View view) {  

  

                StartTime = SystemClock.uptimeMillis();  

                handler.postDelayed(runnable, 0);  

  

                reset.setEnabled(false);  

  

            }  

        });  

  
        pause.setOnClickListener(new View.OnClickListener() {  

            @Override  

            public void onClick(View view) {  

  

                TimeBuff += MillisecondTime;  

  

                handler.removeCallbacks(runnable);  

  

                reset.setEnabled(true);  

  

            }  

        });  

  

        reset.setOnClickListener(new View.OnClickListener() {  

            @Override  

            public void onClick(View view) {  

  

                MillisecondTime = 0L ;  

                StartTime = 0L ;  

                TimeBuff = 0L ;  

                UpdateTime = 0L ;  

                Seconds = 0 ;  

                Minutes = 0 ;  

                MilliSeconds = 0 ;  

  

                timer.setText("00:00:00");  

  

            }  

        });  
  

    }  

  

    public Runnable runnable = new Runnable() {  

  

        public void run() {  

  

            MillisecondTime = SystemClock.uptimeMillis() - StartTime;  

  

            UpdateTime = TimeBuff + MillisecondTime;  

  

            Seconds = (int) (UpdateTime / 1000);  

  

            Minutes = Seconds / 60;  

  

            Seconds = Seconds % 60;  

  

            MilliSeconds = (int) (UpdateTime % 1000);  

  

            timer.setText("" + Minutes + ":"  

                    + String.format("%02d", Seconds) + ":"  

                    + String.format("%03d", MilliSeconds));  

  

            handler.postDelayed(this, 0);  

        }  

  

    };  

  

}  
OUTPUT:

You might also like