Timers in Android Studio using CountDownTimer

See I’m trying to get User input for time and using the user input trying to run the CountDownTimer function in Android studio 4.2.2

The problem is :-

case-1

when I pass Variable name in place of millisInFuture attribute of countDown timer function and trying to set textview accordingly , the TextView doesn’t get set up anything.

public void timer() {
        CountDownTimer countDownTimer = new CountDownTimer(**timeValueIntent**, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                //  mediaPlayer.setLooping(true);
                //mediaPlayer.start();
                timerValueTextView.setText(String.valueOf(millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                Toast.makeText(getApplicationContext(), "Timed out", Toast.LENGTH_SHORT).show();
                restartButton.setVisibility(View.VISIBLE);
                restartButton.setEnabled(true);
                quitButton.setVisibility(View.VISIBLE);
                setBtnCond(true);
                //mediaPlayer.stop();
            }
        }.start();
    }`

case-2

But when I pass int value like 1000 etc in millisInFuture attribute of CountDownTimer and set up the textView , it gets successfully set up.

public void timer() {
        CountDownTimer countDownTimer = new CountDownTimer(**300000**, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                //  mediaPlayer.setLooping(true);
                //mediaPlayer.start();
                timerValueTextView.setText(String.valueOf(millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                Toast.makeText(getApplicationContext(), "Timed out", Toast.LENGTH_SHORT).show();
                restartButton.setVisibility(View.VISIBLE);
                restartButton.setEnabled(true);
                quitButton.setVisibility(View.VISIBLE);
                setBtnCond(true);
                //mediaPlayer.stop();
            }
        }.start();
    }`

Can anyone please help to let me pass variable name instead of a hardcoded integer value in MillisInFuture attribute of CountDownTimer function.!!? `

Answer

I have some source code similar to your questions just implement your project.

Init Variables

private static final long START_TIME_IN_MILLIS = 600000;
private TextView mTextViewCountDown;
private Button mButtonStartPause;
private Button mButtonReset;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;

onCreate

//Init
    mTextViewCountDown = findViewById(R.id.text_view_countdown);
    mButtonStartPause = findViewById(R.id.button_start_pause);
    mButtonReset = findViewById(R.id.button_reset);
    mButtonStartPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mTimerRunning) {
                pauseTimer();       //pauseFunction
            } else {
                startTimer();       //StartFunction
            }
        }
    });



mButtonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTimer();       // resetFunction
            }
        });
        updateCountDownText();      //updateEverySecond

startTimer()

mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMillis = millisUntilFinished;
            updateCountDownText();
        }
        @Override
        public void onFinish() {
            mTimerRunning = false;
            mButtonStartPause.setText("Start");
            mButtonStartPause.setVisibility(View.INVISIBLE);
            mButtonReset.setVisibility(View.VISIBLE);
        }
    }.start();
    mTimerRunning = true;
    mButtonStartPause.setText("pause");
    mButtonReset.setVisibility(View.INVISIBLE);

pauseTimer()

    mCountDownTimer.cancel();
    mTimerRunning = false;
    mButtonStartPause.setText("Start");
    mButtonReset.setVisibility(View.VISIBLE);

resetTimer()

    mTimeLeftInMillis = START_TIME_IN_MILLIS;
    updateCountDownText();
    mButtonReset.setVisibility(View.INVISIBLE);
    mButtonStartPause.setVisibility(View.VISIBLE);

updateCountDownText()

    int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
    int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
    String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
    mTextViewCountDown.setText(timeLeftFormatted);

and Finally XML

<TextView
    android:id="@+id/text_view_countdown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="00:00"
    android:textColor="@android:color/black"
    android:textSize="60sp" />
<Button
    android:id="@+id/button_start_pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_view_countdown"
    android:layout_centerHorizontal="true"
    android:text="start" />
<Button
    android:id="@+id/button_reset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_view_countdown"
    android:layout_marginStart="11dp"
    android:layout_toEndOf="@+id/button_start_pause"
    android:text="reset"
    android:visibility="invisible"
    tools:visibility="visible" />