1

I am using a Timer in my android app.

This is what i am using,

Timer t = new Timer();
 //Set the schedule function and rate
 t.scheduleAtFixedRate(new TimerTask() {

      public void run() 
     {
         //Called each time when 1000 milliseconds (1 second) (the period parameter)
         runOnUiThread(new Runnable() {

                public void run() 
                {
                    TextView tv = (TextView) findViewById(R.id.timer);
                    tv.setText(String.valueOf(time));
                    time += 1;

                }

            });
     }

 },
 //Set how long before to start calling the TimerTask (in milliseconds)
 0,
 //Set the amount of time between each execution (in milliseconds)
 1000); 

In my code, there's only seconds as you can see But I want it in Minutes and seconds like 00:01.

So, how to do it. Please help me.

Thanks in Advance.

Shiv
  • 129
  • 1
  • 9
  • You may find this post useful.- http://stackoverflow.com/questions/9027317/how-to-convert-milliseconds-to-hhmmss-format – ssantos Sep 28 '13 at 13:02
  • @ssantos how to implement that in this code. Can you provide me some code. I have to use this timer in my whole activity and in last in another activity i will show total time taken – Shiv Sep 28 '13 at 13:08
  • Just provided an answer with example. Hope it helps. – ssantos Sep 28 '13 at 13:36

1 Answers1

1

An approach to obtain the String you're looking for, would look like.-

int seconds = time % 60;
int minutes = time / 60;
String stringTime = String.format("%02d:%02d", minutes, seconds);
tv.setText(stringTime);

If you need to show the results only in your second Activity, I'd recommend passing time value into args bundle, and generate the String from the activity which will display it.

ssantos
  • 16,001
  • 7
  • 50
  • 70