1

Basically I'm just trying to get practice on Android Studio so I'm making a flipcoin app.

I want a circle progress bar, which initially is invisible, to became visible for two seconds, and then become invisible again.

With my code, it doesn't become visible at any time.

The goal of the progress bar is just to give some suspense and afterwards I'm going to announce the winner.

@Override
public void onCreate(Bundle savedInstanceState) {

    flipButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Random rd = new Random();
            int result = rd.nextInt(100) + 1;
            winner = result <= oddHeads? Winner.HEADS: Winner.TAILS;
            AnnounceWinner();
        }
    });
 }

private void AnnounceWinner() {
    progressBar.setVisibility(View.VISIBLE);
    i = progressBar.getProgress();
    pause(2000);
    progressBar.setVisibility(View.INVISIBLE);
}

private void pause(int delay) {
    try {
        Thread.sleep(delay);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Fred
  • 11
  • 1
  • 3
    Looks like you are doing the sleep on the UI thread, which you should not. See https://stackoverflow.com/questions/29198262/android-sleep-without-blocking-ui/29198358 – Michael Sep 23 '19 at 21:14
  • So I managed to be able to pause the code with Michael answer but now it leads me to an other problem. Every time I want to pause and do something different after pausing my code, do I need to write an other Runnable? – Fred Sep 23 '19 at 21:48

0 Answers0