0

I have a timer that counts down. I want the displayed format to be 00.00 or "ss.SS". However I haven't made any progress in hours. Without the SimpleDateFormat it displays 01.91 then goes to 01.9. This makes it hard to watch as it flickers to keep the view centered. All I really want is a way to keep the format 01.90 and not allow the 0 to be dropped. Could I accomplish this with my original code without the SimpleDateFormat?

/*
 *  This is my original code before I tried the SimpleDateFormat
 *
 *  This code is fully functional and works good, it just keeps dropping the 0 every
 *     10 milliseconds and makes the view shake
 *
 *  getTimeSecs() could return 5, 10, 15, 30, 90 seconds converted to milliseconds
 *  getCountDownInterval() returns 10
 *  
 */

public void createTimer() {
    myCounter = new CountDownTimer(getTimeSecs(), getCountDownInterval()) {
        public void onTick(long millisUntilFinished) {
        timerIsRunning = true;

            if(millisUntilFinished < 10000) {
                TVcountDown.setText("0" + ((millisUntilFinished / 10) / 100.0));
            } else {
                TVcountDown.setText("" + ((millisUntilFinished / 10) / 100.0));
            }
        } //end onTick()

        @Override
        public void onFinish() {  
            timerIsRunning = false;
            TVcountDown.setBackgroundColor(myRes.getColor(R.color.solid_red));
            TVcountDown.setTextColor(myRes.getColor(R.color.white));
            TVcountDown.setText("Expired");

            // Make sure vibrate feature is enabled
            if(wantsVib == true) {
            vib.vibrate(300);
            }

        } //end onFinish()

    }.start();

} //end createTimer()

Here is my code after trying the SimpleDateFormat

public void createTimer() {
    myCounter = new CountDownTimer(getTimeSecs(), getCountDownInterval()) {
        public void onTick(long millisUntilFinished) {
        timerIsRunning = true;
            long current = (long) ((millisUntilFinished / 10) / 100.0);
            TVcountDown.setText("" + timerDisplay.format(current));
        }

        @Override
        public void onFinish() {  
            timerIsRunning = false;
            TVcountDown.setBackgroundColor(myRes.getColor(R.color.solid_red));
            TVcountDown.setTextColor(myRes.getColor(R.color.white));
            TVcountDown.setText("Expired");

            // Make sure vibrate feature is enabled
            if(wantsVib == true) {
                vib.vibrate(300);
            }

        }

    }.start();

} //end createTimer()

I know! I don't even think I'm close to getting it with the SimpleDateFormat and I'm getting frustrated. It runs, but counts down only seconds, on the milliseconds side. So 15 seconds shows 00.15 not 15.00.

I don't expect someone to code it all out for me just need pointed in the right direction. All the tutorials I can find involve years, days, and such and I can't grasp the concept from that.

I'd prefer not to use the SimpleDateFormat -- cuz it hasn't been to simple for me -- and just use my original code and add a zero to the end of the milliseconds side every 10 milliseconds.

Thanks in advance.

Rockin4Life33
  • 2,240
  • 1
  • 31
  • 29

2 Answers2

2

Try this:

TVcountDown.setText(convertToMyFormat(millisUntilFinished));

and convertToMyFormat() method:

public String convertToMyFormat(long ms) {
    String secString, msecString;
    //constructing the sec format:
    int sec = (int) (ms / 1000);
    if(sec < 10)        secString = "0"+sec;
    else if(sec == 0)   secString = "00";
    else                secString = ""+sec;
    //constructing the msec format:
    int msec = (int) ((ms-(sec*1000))/10.0);
    if(msec < 10)       msecString = "0"+msec;
    else if(msec == 0)  msecString = "00";
    else                msecString = ""+msec;

    return secString+":"+msecString;
}

I'm not sure if I did the msec part correctly but you can tweek it as you want.

iTurki
  • 16,292
  • 20
  • 87
  • 132
0

convert the number to a string and it will keep formatting. additionally you can do something like this

    public String NumToStr(long i){
    if (i < 10 ) {
        return ("0" + Long.toString(i));
    }
        return Long.toString(i);
    }

to make sure "9" will always come back as "09". Now set the string to the text.

actually what might be easier is this

    if(millisUntilFinished < 10000) {
            TVcountDown.setText("0" + Long.toString((millisUntilFinished / 10) / 100.0));
    } else {
            TVcountDown.setText("" + Long.toString((millisUntilFinished / 10) / 100.0));
    }

Use Float.toString() or Double.toString, or whatever you need. Dont be afraid to write a little function to edit the string to make it appear as you want if you need to.

  public String KeepFirstTwoCharOfString(String string){
      //code to store first two Char into string
      // return the string containing only first 2 chars
  }
WIllJBD
  • 6,144
  • 3
  • 34
  • 44
  • Do I need a string array or char array to get all the characters from millisUntilFinished? Or will putting them all in a String allow the timerDisplay.format() to correctly display them? – Rockin4Life33 Aug 18 '12 at 23:29
  • the toString will display any number as its string so a double "1241231.05486405" would be displayed as such. I don't know exactly how all your numbers work, but Log some strings to find out if you need to edit the string at all. Again i would just throw it in a string, take the Chars from the string I want, and then throw that back out. Hours + ":" + Mins + ":" + Secs + ":" + milisecs – WIllJBD Aug 18 '12 at 23:34
  • go here http://stackoverflow.com/questions/11918729/format-stopwatch-to-display-0000/11918811#11918811 if you want some more references/explenation. – WIllJBD Aug 18 '12 at 23:35
  • Thanks, I'm getting closer. If the countdown is greater than 10 seconds it displays "10.111" unless its an even "10.11" then it rounds the last digit off. If the counter is less than 10 seconds it displays "09.99" as I'd like. But again it drops the zero, "09.9" – Rockin4Life33 Aug 19 '12 at 00:16