I want to display the timer in TextView in the format of like [ 19:59].so when i click the start button ,the timer will display like this for example,i want to set upto 20 mintues,it will display like [19:58][19:87].can anyone give some ideas or example code?

- 549
- 2
- 6
- 25
-
4Have you tried anything?? – Krrishnaaaa Jul 05 '13 at 04:11
-
1Can you show us what you have? – Alex Gittemeier Jul 05 '13 at 04:16
-
most of the example stars with starting time like 1,2,3 seconds uptp 60 seconds.i want reverse order – rajeshlawrance Jul 05 '13 at 04:16
-
1@rajeshlawrance check the docs http://developer.android.com/reference/android/os/CountDownTimer.html – Raghunandan Jul 05 '13 at 04:27
-
can u give some example code? – rajeshlawrance Jul 05 '13 at 04:30
-
@rajeshlawrance check my answer. but there is no pause for the timer. you need to cancel and restart. probably store the value when you cancel and provide the value when you restart. – Raghunandan Jul 05 '13 at 04:42
-
@rajeshlawrance did you try the below? – Raghunandan Jul 05 '13 at 09:53
-
Checkout this: https://github.com/Shubhamsdr3/timertextview – pandey_shubham Dec 29 '20 at 13:13
8 Answers
You can use the CountDownTimer
.
http://developer.android.com/reference/android/os/CountDownTimer.html
TextView _tv = (TextView) findViewById( R.id.textView1 );
new CountDownTimer(20*60000, 1000) {
public void onTick(long millisUntilFinished) {
_tv.setText("seconds remaining: " +new SimpleDateFormat("mm:ss:SS").format(new Date( millisUntilFinished)));
}
public void onFinish() {
_tv.setText("done!");
}
}.start();
To cancel just call cancel on the timer.
public final void cancel()
Cancel the countdown.

- 132,755
- 26
- 225
- 256
-
4Can this be used to make a count up timer? i.e. starting from 0 and goes to infinity – faizal Sep 24 '14 at 03:22
-
@RuchirBaronia the first argument takes a long which is milliseconds for testing i used that. You can change it to whatever you feel right – Raghunandan Dec 10 '15 at 04:59
-
So that is how much time the timer will go right? Why not just say: `1200000` instead? – Ruchir Baronia Dec 10 '15 at 05:00
-
-
-
Check the DateFormats in java. https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Raghunandan Dec 10 '15 at 10:48
-
So will this display something like so if I set it at 20 seconds? 20:59 -> 20:58 Every millisecond update? – Ruchir Baronia Dec 14 '15 at 02:36
-
@RuchirBaronia yes. timer task always run in background and with callbacks you update ui – Raghunandan Dec 14 '15 at 10:53
-
So will this display something like so if I set it at 20 seconds? 20:59 -> 20:58 Every millisecond update? – Ruchir Baronia Dec 14 '15 at 10:59
-
-
Reason I'm asking is because whenever I use count down timer, it is inaccurate. It will go by 12 or 15 milliseconds off everytime. That is why I'm asking, is this accurate? – Ruchir Baronia Dec 14 '15 at 11:15
package com.example.testproject;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
public int seconds = 60;
public int minutes = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv = (TextView) findViewById(R.id.main_timer_text);
tv.setText(String.valueOf(minutes)+":"+String.valueOf(seconds));
seconds -= 1;
if(seconds == 0)
{
tv.setText(String.valueOf(minutes)+":"+String.valueOf(seconds));
seconds=60;
minutes=minutes-1;
}
}
});
}
}, 0, 1000);
}
}

- 549
- 2
- 6
- 25
-
this is the code i tried and i got the answer with my own logic – rajeshlawrance Jul 05 '13 at 10:16
-
little bit mistake: second -= 1 must be after if(seconds == 0){...}, else your timer's second will show 0 after 2 – Daryn Nov 21 '13 at 04:25
-
2
do it this way activity_timer.xml
<android.support.v7.widget.LinearLayoutCompat
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Chronometer
android:id="@+id/chronometer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.LinearLayoutCompat>
And in Activity
public class TimerActivity extends AppCompatActivity {
private Chronometer chronometer2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
chronometer2 = findViewById(R.id.chronometer2);
chronometer2.start();
}
}
You can also use stop
chronometer2.stop();
Re Start
chronometer2.setBase(SystemClock.elapsedRealtime());
chronometer2.start();

- 900
- 10
- 7
//start button click
CountDown timer = new CountDown(180000, 1000);
timer.start();
//stop button click
timer.stop();
//countdown class
public class CountDown extends CountDownTimer {
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long ms = millisUntilFinished;
String text = String.format("%02d\' %02d\"",
TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
textViewTimer.setText(text);
}
@Override
public void onFinish() {
textViewTimer.setText("ffinish");
}
}

- 4,828
- 2
- 24
- 30
Here I have use Timer class to display timer
public class FourthActivity extends AppCompatActivity {
Button startButton, pauseButton;
TextView timerValue;
Timer timer;
int seconds = 0, minutes = 0, hour = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
bindView();
timer = new Timer();
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (seconds == 60) {
timerValue.setText(String.format("%02d", hour) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds));
minutes = seconds / 60;
seconds = seconds % 60;
hour = minutes / 60;
}
seconds += 1;
timerValue.setText(String.format("%02d", hour) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds));
}
});
}
}, 0, 1000);
}
});
}
private void bindView() {
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
}
}
In layout, there are one TextView and one Button to start timer. I have use Timer class method scheduleAtFixedRate
. Time will be displayed in hh:mm:ss form.

- 1,270
- 2
- 16
- 32

- 1,042
- 13
- 22
-
1not correct, minutes can not increase more than 1. you should ++ it in if statement, same problem for hours – Arash Jul 25 '18 at 07:22
Showing timer in Kotlin.
class TimerDemo : AppCompatActivity() {
private lateinit var binding: ActivityTimerDemoBinding
private lateinit var timer: Timer
private var seconds = 0
private var secondsToDisplay = 0
private var minutes = 0
private var hours = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityTimerDemoBinding.inflate(layoutInflater)
setContentView(binding.root)
configureTimer()
}
private fun configureTimer() {
timer = Timer().apply {
scheduleAtFixedRate(object : TimerTask() {
override fun run() {
runOnUiThread {
if (seconds % 60 == 0) {
binding.tvTimerValue.text =
"${String.format("%02d", hours)} :" +
" ${String.format("%02d", minutes)} :" +
" ${String.format("%02d", secondsToDisplay)}"
minutes = seconds / 60
secondsToDisplay %= 60;
hours = minutes / 60
}
seconds++
secondsToDisplay++
binding.tvTimerValue.text =
"${String.format("%02d", hours)} :" +
" ${String.format("%02d", minutes)} :" +
" ${String.format("%02d", secondsToDisplay)}"
}
}
}, 0, 1000)
}
}
override fun onStart() {
super.onStart()
binding.btnStopTimer.setOnClickListener { timer.cancel() }
}
}

- 450
- 6
- 11
This can be done using cronometer, the most main reason is supporting API 1+, which is quite impressive rather than TextClock, which supports API 17+.
You can do lots of other things with cronometer. for example you can set start time in it
chronometer.setBase(SystemClock.elapsedRealtime());
You can also learn about it more here.

- 739
- 8
- 13
I made code for timer display in textView.The formate is for e.g:- 02:59:00
You can follow this link to get the Step By Step
Code.

- 273
- 1
- 3
- 9