Search code examples
javatextviewcountdowntimer

How can I fix this code so my android textview updates with the countdowntimer and shows the seconds that are left?


I am trying to make the countdown app from the movie Countdown. I am very new to coding. The goal here is to update the textview that shows the number of seconds left to live. Here is the java code I wrote to try to accomplish that. I know that the other aspects such as years, days, hours and minutes aren't finished yet.


import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    private TextView mTextViewCountDownYears;
    private TextView mTextViewCountDownDays;
    private TextView mTextViewCountDownHours;
    private TextView mTextViewCountDownMinutes;
    private TextView mTextViewCountDownSeconds;

    private Button mButtonAccept;

    private CountDownTimer mCountDownTimer;

    private Boolean mTimerRunning;

    long timeLeftToLive = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar deathDay = Calendar.getInstance();
        deathDay.set(2021,7,24,10,14,45);
        Calendar today = Calendar.getInstance();

        long diff = deathDay.getTimeInMillis() - today.getTimeInMillis();
        timeLeftToLive = diff;

        mTextViewCountDownYears = findViewById(R.id.text_view_countdown_years);
        mTextViewCountDownDays = findViewById(R.id.text_view_countdown_days);
        mTextViewCountDownHours = findViewById(R.id.text_view_countdown_hours);
        mTextViewCountDownMinutes = findViewById(R.id.text_view_countdown_minutes);
        mTextViewCountDownSeconds = findViewById(R.id.text_view_countdown_seconds);

        mButtonAccept = findViewById(R.id.button_accept);

        mButtonAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();
            }
        });
    }
    private void startTimer(){
        mCountDownTimer = new CountDownTimer(timeLeftToLive, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftToLive = millisUntilFinished;
                updateTextView();
            }

            @Override
            public void onFinish() {

            }
        };
    }
    private void updateTextView() {
        long secondsLeft = timeLeftToLive / 1000;

        String secondsLeftFormatted = String.format("%02d",secondsLeft);

        mTextViewCountDownSeconds.setText(secondsLeftFormatted);
    }
}

Solution

  • Your code looks good and you are so close to it working properly! You simply need to add a .start() to the end of your definition of CountDownTimer inside your startTimer() function (before the semi-colon). Like this:

    private void startTimer(){
        mCountDownTimer = new CountDownTimer(timeLeftToLive, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftToLive = millisUntilFinished;
                updateTextView();
            }
    
            @Override
            public void onFinish() {
    
            }
        }.start();
    }
    

    There's a great example for you to reference here if you need it