Search code examples
androidtimercountdowncountdowntimersettext

Android CountDownTimer is too fast for textView update


im trying to implement timer using android, such that it will countdown from given number to 0, to do it i use CountDownTimer... the problem is that i want it to be updated every 1 millisecond and it seems that the textView that i use to present the time is to slow when i try to setText... for example if i start the timer from 20000 and se 1 millisecond as countDownInterval the textView stops changing when it gets to approx 19000..

here is my code:

currTimer=new CountDownTimer(20000,10) {

            @Override
            public void onTick(long millisUntilFinished) {
                scoreBoard.setCurrMilliTime(scoreBoard.getCurrMilliTime()-10);
                textView.setText(Integer.toString(scoreBoard.getCurrMilliTime()));

            }

            @Override
            public void onFinish() {
                scoreBoard.setCurrMilliTime(scoreBoard.getCurrMilliTime()-10);
                textView.setText(Integer.toString(scoreBoard.getCurrMilliTime()));

            }
        }.start();

Solution

  • I just tried this :

    final TextView textView1 = (TextView) findViewById(R.id.textView1);
        new CountDownTimer(20000,10) {
    
            @Override
            public void onTick(long millisUntilFinished) {
                textView1.setText(String.format("%d : %d :%d", 
                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
    
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)), 
    
                        TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - 
                        TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))
                    ));
    
            }
    
            @Override
            public void onFinish() {
    
            }
        }.start();
    

    and works perfectly, hope this helps, although i don't think a step by "10" is average enough, try 100