You are on page 1of 3

XML File:-

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
android:id="@+id/textViewTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="00:00:000"
android:padding="15dp"
android:layout_gravity="center_horizontal"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:orientation="vertical"
android:gravity="center">

<Button
android:id="@+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#494a49"
android:layout_marginStart="16dp"
android:text="Start"
app:icon="@android:drawable/ic_media_play" />

<Button
android:id="@+id/buttonPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:backgroundTint="#494a49"
android:text="Pause"
app:icon="@android:drawable/ic_media_pause" />

<Button
android:id="@+id/buttonReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:backgroundTint="#494a49"
android:text="Reset"
app:icon="@android:drawable/ic_popup_sync" />
</LinearLayout>

</LinearLayout>
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
JAVA File :-
package com.example.practical5;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


private TextView textViewTimer;
private Button buttonStart;
private Button buttonPause;
private Button buttonReset;
private long startTime = 0L;
private long pauseTime = 0L; // Variable to store paused time
private Handler handler = new Handler();
private long timeMilliseconds = 0L;
private long updateTime = 0L;
private Runnable updateTimeThread = new Runnable() {
public void run() {
timeMilliseconds = System.currentTimeMillis() - startTime +
pauseTime; // Add pause time
updateTime = timeMilliseconds;
int secs = (int) (updateTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updateTime % 1000);
textViewTimer.setText("" + mins + ":" + String.format("%02d", secs) +
":"
+ String.format("%03d", milliseconds));
handler.postDelayed(this, 0);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textViewTimer = findViewById(R.id.textViewTimer);
buttonStart = findViewById(R.id.buttonStart);
buttonPause = findViewById(R.id.buttonPause);
buttonReset = findViewById(R.id.buttonReset);

buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTime = System.currentTimeMillis();
handler.postDelayed(updateTimeThread, 0);
buttonPause.setEnabled(true);
buttonStart.setEnabled(false);
buttonReset.setEnabled(true);
}
});

buttonPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pauseTime += System.currentTimeMillis() - startTime; // Add elapsed
time to pause time
handler.removeCallbacks(updateTimeThread);
buttonPause.setEnabled(false);
buttonStart.setEnabled(true);
buttonReset.setEnabled(true);
}
});

buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.removeCallbacks(updateTimeThread);
textViewTimer.setText("00:00:000");
buttonPause.setEnabled(false);
buttonStart.setEnabled(true);
buttonReset.setEnabled(false);
startTime = 0L;
pauseTime = 0L; // Reset pause time
}
});
}
}

You might also like