Android Create CountDown Timer and StopWatch with start, stop and pause button
We are working on one of our client app in which we need to manage tasks. For each tasks we need to create a timer or stopwatch countdown based on time is pre-assigned or not. This Article is going to show How to create a simple Timer or StopWatch CountDown app (Not going to use internal app for the same purpose).
Before start below is the demo about what we are going to achieve in this article.
As shown in above video in our main layout we have two textview, with 8 buttons.
Below is the code for activiyt_main.xml file.
Below is the code for MainActivity.java file.
Finally it's time to launch our app. Below is the screenshot for first execution.
https://android--examples.blogspot.in/2015/04/android-countdowntimer-start-pause.html
Before start below is the demo about what we are going to achieve in this article.
As shown in above video in our main layout we have two textview, with 8 buttons.
Below is the code for activiyt_main.xml file.
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" tools:context="com.vlemonn.blog.countdowntimerandstopwatch.MainActivity"> <TextView android:id="@+id/tvElapsedTime" android:layout_width="0dp" android:text="Click on Start Button to Start Timer" android:layout_height="wrap_content" android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="8dp" android:gravity="center" app:layout_constraintRight_toRightOf="parent" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:orientation="horizontal" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="7dp" app:layout_constraintTop_toBottomOf="@+id/tvElapsedTime" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" android:id="@+id/linearLayout" android:gravity="center" > <Button android:id="@+id/btnStartTimer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" /> <Button android:id="@+id/btnPauseTimer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" /> <Button android:id="@+id/btnResumeTimer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Resume" /> <Button android:id="@+id/btnStopTimer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop Reset" /> </LinearLayout> <TextView android:id="@+id/tvRemainingTime" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="32dp" app:layout_constraintTop_toBottomOf="@+id/linearLayout" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="8dp" android:text="Stop Watch 10 Secs" android:gravity="center" app:layout_constraintRight_toRightOf="parent" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="8dp" android:gravity="center" app:layout_constraintTop_toBottomOf="@+id/tvRemainingTime" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent"> <Button android:id="@+id/btnStartStopWatch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" /> <Button android:id="@+id/btnPauseStopWatch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" /> <Button android:id="@+id/btnResumeStopWatch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Resume" /> <Button android:id="@+id/btnStopStopWatch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
Below is the code for MainActivity.java file.
package com.vlemonn.blog.countdowntimerandstopwatch; import android.os.CountDownTimer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private TextView tvElapsedTime,tvRemainingTime; private Button btnStartTimer,btnPauseTimer,btnResumeTimer,btnStopTimer; private Button btnStartStopWatch,btnPauseStopWatch,btnStopStopWatch,btnResumeStopWatch; CountDownTimer countDownTimer; long millisInFuture = 10000; //10 seconds long countDownInterval = 1000; //1 second long stopWatchTimeRemaining=0; boolean isPaused=false,isStopped=false; Timer timer; long timerInterval = 1000; //1 second long timerDelay = 1000; //1 second int Count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvElapsedTime = (TextView) findViewById(R.id.tvElapsedTime); tvRemainingTime= (TextView) findViewById(R.id.tvRemainingTime); btnStartTimer = (Button) findViewById(R.id.btnStartTimer); btnPauseTimer= (Button) findViewById(R.id.btnPauseTimer); btnResumeTimer = (Button) findViewById(R.id.btnResumeTimer); btnStopTimer= (Button) findViewById(R.id.btnStopTimer); stopWatchTimeRemaining = millisInFuture; btnStartTimer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startTimer(); } }); btnPauseTimer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pauseTimer(); } }); btnResumeTimer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resumeTimer(); } }); btnStopTimer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopTimer(); } }); btnStartStopWatch = (Button) findViewById(R.id.btnStartStopWatch); btnPauseStopWatch= (Button) findViewById(R.id.btnPauseStopWatch); btnStopStopWatch = (Button) findViewById(R.id.btnStopStopWatch); btnResumeStopWatch= (Button) findViewById(R.id.btnResumeStopWatch); btnStartStopWatch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopWatchTimeRemaining = millisInFuture; startStopWatch(); } }); btnPauseStopWatch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pauseStopWatch(); } }); btnStopStopWatch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopStopWatch(); } }); btnResumeStopWatch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resumeStopWatch(); } }); } private void resumeStopWatch() { isPaused=false; isStopped=false; startStopWatch(); } private void stopStopWatch() { isPaused=false; isStopped=true; tvRemainingTime.setText("Stopped after " + (((millisInFuture - stopWatchTimeRemaining)/1000)+1) + " Time is Reset back to 10 seconds" ); stopWatchTimeRemaining = millisInFuture; } private void pauseStopWatch() { isPaused=true; } private void startStopWatch() { isPaused=false; isStopped=false; countDownTimer = new CountDownTimer(stopWatchTimeRemaining,countDownInterval) { @Override public void onTick(long millisUntilFinished) { if(isPaused || isStopped) { //If the user request to cancel or paused the //CountDownTimer we will cancel the current instance cancel(); } else { //Display the remaining seconds to app interface //1 second = 1000 milliseconds tvRemainingTime.setText("Remaining Time : " + (millisUntilFinished / 1000) + " Seconds"); //Put count down timer remaining time in a variable stopWatchTimeRemaining = millisUntilFinished; } } @Override public void onFinish() { tvRemainingTime.setText("Your allotted time is finished : "+(millisInFuture/1000)+" Seconds"); } }.start(); } private void stopTimer() { tvElapsedTime.setText("Stopped after " + Count + " s"); Count=0; pauseTimer(); } private void resumeTimer() { startTimer(); } private void startTimer() { timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { tvElapsedTime.setText(Count + " s"); Count++; } },timerDelay,timerInterval); } private void pauseTimer(){ if(timer!=null) { timer.cancel(); } } }
Finally it's time to launch our app. Below is the screenshot for first execution.
https://android--examples.blogspot.in/2015/04/android-countdowntimer-start-pause.html
Android Create CountDown Timer and StopWatch with start, stop and pause button
Reviewed by Mayank Sanghvi
on
November 09, 2017
Rating:
No comments: