0

I am implementing counter of 15,then i split the counter value so that i can set "1" in one text view and "5" in another text view.Same is with 14 "1" in one text view and "4"in another.But what it does is not showing "14" from "15" to it jumps to "13".Please help me.Below is my code

Code:-

handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                MainActivity.help_icon.setClickable(false);

                new CountDownTimer(15000,1000) {

                    public void onTick(long millisUntilFinished) {
                        hideDigits();
                        hideRowOneTwoFourSixElements();
                        digit2.setVisibility(View.VISIBLE);
                        digit3.setVisibility(View.VISIBLE);
                        digit4.setVisibility(View.VISIBLE);
                        digit2.setText("0:");

                        String count=""+millisUntilFinished / 1000;
                        String[] mArray = count.split("");
                        Log.i("tag","count outside->"+count);

                        if(count.length()==1)
                        {
                            digit3.setVisibility(View.VISIBLE);
                            digit3.setText("0");
                            try
                            {
                                digit4.setText(mArray[1]);
                            }
                            catch (Exception e){}
                        }

                        else
                        {
                            try
                            {
                                Log.i("tag","count inside->"+count + "array 1-->"+ mArray[1]);
                                digit3.setText(mArray[1]);
                            }
                            catch (Exception e)
                            {

                            }


                            try{
                                Log.i("tag","count inside->"+count + "array 2-->"+ mArray[2]);
                                digit4.setText(mArray[2]);
                            }catch (Exception e){}
                        }



                    }

                    public void onFinish() {
                        mode_button.setEnabled(true);
                        isRunning=true;
                        mainDisplay();
                    }
                }.start();

            }
        }, 7000);



    }
shivani gupta
  • 235
  • 4
  • 20

2 Answers2

2

You should be using mArray[0] and mArray[1], not mArray[1] and mArray[2]. Remember, arrays start at 0.

lionscribe
  • 3,413
  • 1
  • 16
  • 21
0

This is a known bug related with CountDownTimer. Timer skips one tick while executing the timer. You could see some workarounds in related posts. And the one which is better is to set the countDownInterval to 500 instead of 1000.

Read more solutions here. Android: CountDownTimer skips last onTick()!

Community
  • 1
  • 1
Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
  • Probably your point to change to 500 is the correct solution, as the countdown isn't exact to the millisecond, and sometimes an intermission of 1001 would skip a second.. I would probably set it even lower, for a smoother countdown. He should cache the seconds value of last update, so if seconds don't change, there is no need to update screen. – lionscribe Dec 14 '16 at 13:09