views:

195

answers:

3

I'm looking for a way to make the Chronometer in Android (preferably 1.6 and upwards) show 10ths of a second while counting up.

Is it possible to do this? If not, is there a free (and preferably open source) library that does the same? Failing that I'll write my own, but I'd rather use someone else's!

+1  A: 

Is it possible to do this?

Not really. You could pass a format string that shows tenths of a second, but the Chronometer itself only updates every second. The update frequency is baked into the code.

If not, is there a free (and preferably open source) library that does the same?

The Chronometer source is available under the Apache License 2.0, so you can modify it to suit. Find all occurrences of 1000 and change them to 100, and you're probably most of the way there.

Bear in mind that this Chronometer can be used in an activity but not an app widget, since customized View classes are not supported by the app widget framework.

CommonsWare
A: 

Hey Matthew, did you manage to display the milliseconds? I tried to modify the chronometer source, but after some tries in which it did not work, I ended up having my project not being able to be converted to Dalvik (as Eclipse stated). I would need to have the chronometer tick every millisecond if possible, because I want to develop a clapboard application in which I need the number of frames (which of course, should be derived from the number of milliseconds). If anyone has any idea, please post it. Thanks.

bboylalu
I'm afraid that I didn't in the end. Because I was only writing a game I just went with the number of seconds as that was accurate enough.Good luck!
Matthew Steeples
A: 

I have found the way after lots and lots of research. I don't know if it's effective or not, because the truth is that the Emulator crashes after about 10 seconds, but on the phone it runs just fine, and that does it for me. It looks something like this:

import android.os.Handler;
public class Chronometer extends Activity {
  private Handler mHandler = new Handler();
public void actions() {
  mHandler.removeCallbacks(mUpdateTimeTask);
  mHandler.postDelayed(mUpdateTimeTask, 1); //1 is the number of milliseconds you want the text to be updated
}
Runnable mUpdateTimeTask = new Runnable() {
  public void run() {
    i++;
    txt.setText(formatTime(i)); // formatTime is just for make it readable in HH:MM:SS:MS, but I assume you already have this
    mHandler.postDelayed(this, 1);
    }
  };
}
bboylalu