android string.format not formatting correctly with a timer

491 Views Asked by At

I have a textview that displays a timer, and i use string.format so the seconds will always use 2 digits: 0:00

But when i hit 10 minutes(the minutes also goes up to two digits) the seconds only uses one digit instead of two.

The timerRunnable:

Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {


        millis = System.currentTimeMillis() - startTime;

        aSeconds = (int) (millis / 1000) + paSeconds;


        seconds = (int) (millis / 1000) + pSeconds;
        minutes = seconds / 60 + pMinutes;
        seconds = seconds % 60;



        timerTextView.setText(String.format("%d:%02d", minutes, seconds));

        if(aSeconds == delay){

            Toast.makeText(getApplicationContext(), "Drink",
                    Toast.LENGTH_LONG).show();

            delay += delay;
            count += 1;

            Log.d("MainActivity", "" + count);

        }

        timerHandler.postDelayed(this, 1000);


    }
};

I'm pretty sute the problem is that I set the String.format to basically only include 3 digits combined, but i can't find a clever solution to the problem

2

There are 2 best solutions below

0
On BEST ANSWER

The solution was simply to just make my textview smaller when the timer hits 10 minutes, it just wasn't showing on the screen..

1
On

did you try the if..else?

if (minute >=10 && minute<=59)
   timerTextView.setText(String.format("02%d:%02d", minutes, seconds));

else       
   timerTextView.setText(String.format("2%d:%02d", minutes, seconds));

also whats pSeconds?