user1762507
user1762507

Reputation: 782

How can you make a stopwatch with the chronometer that shows milliseconds?

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;

    }
}

}; }

Upvotes: 1

Views: 1048

Answers (1)

quietmint
quietmint

Reputation: 14164

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.

Upvotes: 1

Related Questions