I'm trying to implement a decreasing counter using for loop in android java. I have used handler & runnable to delay for loop iteration and I want the counter to start from 80 and end on 0, but in output, I'm getting counter from 0 to 80. In short, the reverse is required.
This is my code,
TextView totalpoints = (TextView) findViewById(R.id.txttotalpoints);
Handler handler1 = new Handler();
for (int count = 80;count>=0; count--){
int finalCount = count;
handler1.postDelayed(new Runnable() {
@Override
public void run() {
totalpoints.setText("Total Points : "+ finalCount);
System.out.println("This is in newpointsCounter" + finalCount);
}
}, 1000 * count);
}
Current output => Start from 0 & end on 80
Required output => Start from 80 & end on 0
You can user CountDownTimer as:
CountDownTimer countDownTimer = new CountDownTimer(80000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//TODO on each interval, print your count
}
@Override
public void onFinish() {
//TODO on finish of timer, you will get notified
}
};
countDownTimer.start();