-2

I am trying to set a for loop with a delay using the handler thread. But when i set up the handler thread within the for loop i cannot use the index of the for loop as it says it must be called final, however that does not work either. Does anyone know how to fix this?

NojDavid
  • 89
  • 1
  • 10

2 Answers2

1

To Sleep Thread for while time you have to set for loop outside of postDelayed like below way. In that way you can acheive the Index of the loop.

final ImageButton[] all= {btn1, btn2, btn3, btn4};
Handler handler1 = new Handler();
for (int a = 1; a<= all.length ;a++) {
    handler1.postDelayed(new Runnable() {

         @Override
         public void run() {

             //Here..
         }
         }, 1000 * a);
    } 
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
0

I think you can use CountDownTimer:

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
       //here you can have your logic to set text to edittext
    }

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

}.start();

How to make a countdown Timer in android?

Or even like this:

public void animateTextView(float initialValue, float finalValue, final TextView  textview) {

    ValueAnimator valueAnimator = ValueAnimator.ofFloat(initialValue, finalValue);
    valueAnimator.setDuration(1500);

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {

            textview.setText(valueAnimator.getAnimatedValue().toString());

        }
    });
    valueAnimator.start();

}
Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40