0

I'm trying to loop my countdown timer for a specific number of times, but I'm not sure where I should add my for loop... Currently trying to do an interval timer function (I know there's a way to do it via Handler, but I'm still a beginner and got kind of confused on how I shoul use the handler)

I've tried adding it at '.start' and the StartTimer function but the countdown time still stays at 0. Would be great if any assistance is given as I'm still a beginner.. Thanks!

package com.example.bushykai.myapplication;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Timer extends AppCompatActivity {
private static final long START_TIME_IN_MILLIS=10000;
private int sets = 3;
private TextView textViewCountdown;
private Button buttonStartPause;
private Button buttonReset;

private CountDownTimer countDownTimer;

private boolean timerRunning;

private long timeLeftInMillis = START_TIME_IN_MILLIS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);

    textViewCountdown = findViewById(R.id.countdownTimer);
    buttonStartPause = findViewById(R.id.startPause);
    buttonReset= findViewById(R.id.reset);

    buttonStartPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (timerRunning) {
                pauseTimer();
            }

            else {

                    startTimer();


            }
        }
    });

    buttonReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetTimer();
        }
    });

    updateCountDownText();
}

private void pauseTimer() {
    countDownTimer.cancel();
    timerRunning = false;
    buttonStartPause.setText("Start");
    buttonReset.setVisibility(View.VISIBLE);
}

private void startTimer() {
    countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            timeLeftInMillis = millisUntilFinished;
            updateCountDownText();
        }

        @Override
        public void onFinish() {
            timerRunning = false;
            buttonStartPause.setText("Start");
            buttonStartPause.setVisibility(View.INVISIBLE);
            buttonReset.setVisibility(View.VISIBLE);

        }
    }.start();
    timerRunning = true;
    buttonStartPause.setText("Pause");
    buttonReset.setVisibility((View.INVISIBLE));

}

private void resetTimer() {

    timeLeftInMillis = START_TIME_IN_MILLIS;
    updateCountDownText();
    buttonReset.setVisibility(View.INVISIBLE);
    buttonStartPause.setVisibility(View.VISIBLE);

}

private void updateCountDownText() {
    int minutes = (int) (timeLeftInMillis / 1000 ) / 60;
    int seconds = (int) (timeLeftInMillis / 1000 ) % 60;

    String timeLeftFormatted = String.format("%02d:%02d", minutes, seconds);

    textViewCountdown.setText(timeLeftFormatted);

}
}
bushykai
  • 3
  • 2

1 Answers1

2

You have to add a Runnable Handler to your class try code below and modify with your Countdown Timer

public Handler timerHandler = new Handler();
public Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {
        //show alert or DO Whatever
    }
    };


public void timerStart() {
    timerStop();
    timerHandler.postDelayed(timerRunnable, Constants.TIMER_TIME_OUT);
}

public void timerStop() {
    timerHandler.removeCallbacks(timerRunnable);
}