0

I am working on a very simple card WAR game APP, and i wanted to display on a textview (3,2,1 -> WAR!). I've tried this method:

public void doCounting(){
    for(int i=3;i>=0;i--)
    {
        if(i!=0){
        waitTv.setText("Wait "+i);
        SystemClock.sleep(1000);
        }else{
            waitTv.setText("WAR!");
        }

    }
}

It didnt worked it just make my app stuck for three seconds then enters the activity and place "WAR!" in the textView..

3 Answers3

0

The right way to do it is to use the Thread.sleep(long milliseconds), although this method could throw an exception so you need to surround it in a try and catch like this.

try{
    Thread.sleep(3000);
}catch(Exception e){
    e.printStackTrace();
}
b3zman41
  • 1
  • 1
-1

You can use this code:

CountDownTimer countDownTimer = new CountDownTimer(3, 1000){

        @Override
        public void onTick(long millisUntilFinished) {
            waitTv.setText("Wait " + millisUntilFinished/1000);
        }

        @Override
        public void onFinish() {
            waitTv.setText("WAR!");
        }
    }.start();
Mian.Ammar
  • 663
  • 1
  • 9
  • 22
-1
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Stopwatch {
static int interval;
static Timer timer;

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Input seconds => : ");
    String secs = sc.nextLine();
    int delay = 1000;
    int period = 1000;
    timer = new Timer();
    interval = Integer.parseInt(secs);
    System.out.println(secs);
    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            System.out.println(setInterval());

        }
    }, delay, period);
}

private static final int setInterval() {
    if (interval == 1)
        timer.cancel();
    return --interval;
}
}

Already on Stackoveflow how to make a countdown timer in java

Community
  • 1
  • 1
stuckedunderflow
  • 3,551
  • 8
  • 46
  • 63