Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CountDownTimer Start Button

I got a problem with CountDownTimer's StartButton: the timer doesn't start after pressing the button. How do I fix that?

I want to start the timer by pressing button buttonCount. Can someone help me please?

int clicks = 0;
TextView textCount;
ImageButton buttonCount;
int guessCount =0;
boolean started = false;
boolean timerProcessing = false;
private CountDownTimer count;



protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newgame);

        count = new CountDownTimer(15000, 1000) {
            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished / 1000));
                textic.setText("Time Left: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                textic.setText("Time's Up!");
                buttonCount.setEnabled(false);
                if (clicks > oldscore)
                    getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
            }
        };

        final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
        final TextView textView = (TextView) findViewById(R.id.applesEaten);

        buttonCount = (ImageButton) findViewById(R.id.button);
        buttonCount.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                clicks++;
                textView.setText("Clicks: " + clicks);
                TextView textView = (TextView) findViewById(R.id.topScoreView);
                textView.setText("Best: " + oldscore);

                if(!started){
                    count.start();
                    started = true;
                    timerProcessing = true;
                }
            }
        });
        final TextView textic = (TextView) findViewById(R.id.textView2);
    }
like image 550
Matt.K Avatar asked Jul 24 '26 00:07

Matt.K


1 Answers

It seems to me this is what you really want to do:

    private int clicks = 0;
    private TextView textCount;
    private ImageButton buttonCount;
    private int guessCount = 0;  
    private CountDownTimer count; // RENAMED     
    private boolean started = false; // FALSE. 
    private boolean timerProcessing = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newgame);

        count = new CountDownTimer(15000, 1000) { // MOVED UP
            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished / 1000));
                textic.setText("Time Left: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                textic.setText("Time's Up!");
                buttonCount.setEnabled(false);
                if (clicks > oldscore)
                    getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
            }
        };


        final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
        final TextView textView = (TextView) findViewById(R.id.applesEaten);

        buttonCount = (ImageButton) findViewById(R.id.button);
        buttonCount.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                clicks++;
                textView.setText("Clicks: " + clicks);
                TextView textView = (TextView) findViewById(R.id.topScoreView);
                textView.setText("Best: " + oldscore);

                if(!started){
                    count.start(); // START COUNTDOWN TIMER
                    started = true;
                    timerProcessing = true;

                }

            }
        });
        final TextView textic = (TextView) findViewById(R.id.textView2);


    }

And if you really want to start another CountDownTimer than the one you create at the bottom (named count). Then you need to instantiate it and set its behaviour, just like you do for the other CountownTimer.

Also, all the variables you use need to be created before (textic, oldscore)

like image 99
Christoffer Avatar answered Jul 25 '26 12:07

Christoffer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!