Search code examples
androidmultithreadingandroid-activitydelaysleep

Want to display number with 1sec delay


I want to display numbers in a TextView like this:

Wait 5 sec // then a delay of 1 sec
Wait 4 sec // display this in the same text view along with delay
Wait 3 sec // display this in the same text view along with delay
Wait 2 sec // display this in the same text view along with delay
Wait 1 sec // display this in the same text view along with delay

I want perform this action on a button click and it should work like a count down timer.


Solution

  • You can use this one

    new CountDownTimer(30000, 1000) {
    
         public void onTick(long millisUntilFinished) {
             mTextField.setText("Wait: " + millisUntilFinished / 1000);
         }
    
         public void onFinish() {
             mTextField.setText("done!");
         }
      }.start();
    

    This timer class can start at the time of button click and also you can adjust the total time and the tick interval

    This is from developer site