Roonil
Roonil

Reputation: 536

Sequence of actions with delays android

I'm trying to get my app to display a sequence of images, 1 second after the other. Currently my java looks like this:

    arrow1.setVisibility(View.VISIBLE);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            arrow1.setVisibility(View.INVISIBLE);
            arrow2.setVisibility(View.VISIBLE);
        }
    }, 1000);
    handler.postDelayed(new Runnable() {
        public void run() {
            arrow2.setVisibility(View.INVISIBLE);
            arrow3.setVisibility(View.VISIBLE);
        }
    }, 1000);

I'm not getting any errors, but it's also not working as I intended. Arrow2 is not displaying at all, the app is going straight from arrow1 to arrow3 with a slight delay. Is my second handler.postDelayed(new Runnable() function being overriden? How should I best go about having a delay in this scenario?

Upvotes: 3

Views: 207

Answers (2)

ugur
ugur

Reputation: 3654

You can also use CountDownTimer as below. See official doc for more details.

Set millisInFuture to countDownInterval*3 for 3 images and set countDownInterval for delay between images.

long countDownInterval = 1000; // 1sec interval
long millisInFuture = countDownInterval*10; // 10sec total time
new CountDownTimer(millisInFuture, countDownInterval) {

     public void onTick(long millisUntilFinished) {
        arrow1.setVisibility(millisUntilFinished < millisInFuture ?  View.VISIBLE:View.INVISIBLE);
        arrow2.setVisibility(millisUntilFinished > 0 ?  View.VISIBLE:View.INVISIBLE);
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

Upvotes: 0

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11642

You can try like this,

private static final int TotalLoopCount = 2;

private int count  = 0;
private int mCurrentLoopCount = 0;

Handler handler = new Handler();  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Your code

}


@Override
protected void onResume() {
    super.onResume();

    handler.postDelayed(runnable, 0);
}

@Override
protected void onPause() {
    super.onPause();
    handler.removeCallbacks(runnable);
}

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        arrow1.setVisibility(View.INVISIBLE);
        arrow2.setVisibility(View.INVISIBLE);
        arrow3.setVisibility(View.INVISIBLE);

        if(count == 0) {
            arrow1.setVisibility(View.VISIBLE);
        } else if(count == 1) {
            arrow2.setVisibility(View.VISIBLE);
        } else {
            arrow3.setVisibility(View.VISIBLE);
        }

        count++;

        if(count == 3) {
            count = 0;

            mCurrentLoopCount++;
        }

        if(mCurrentLoopCount < TotalLoopCount) {
            handler.postDelayed(runnable, 3000);
        }
    }
};

Upvotes: 1

Related Questions