Question

Hi I'm trying to make a scoreboard but I can't figure out how to get it to display milliseconds. In my xml a textview that displays ".000" and in my main java class I have

   public class MainActivity extends Activity {
   timeEx = (TextView) findViewById(R.id.timeEx);
ending= 000;

View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {

    if (running == true){


    }else{

    MyChronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
    MyChronometer.start();
    running = true;
     TimeEnd();

   }

    }
}

private void TimeEnd() {
    // TODO Auto-generated method stub
    while(running == true){

        ending ++;
        timeEx.setText("."+ ending );
        if (ending == 999)
            ending = ending - 999;

    }
}

}; }

Was it helpful?

Solution

elapsedRealtime() already is in milliseconds (specifically, "milliseconds since the system was booted, including deep sleep").

If you want to display the elapsed time as seconds with a decimal point (e.g., the TextView should show "2.198"), divide the calculated time difference by 1000. If you only want the milliseconds portion (e.g., the TextView should show "198"), then use the modulo operator to get the remainder of division by 1000.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top