ReachmeDroid
ReachmeDroid

Reputation: 979

How to Display the time elapsed onto UI Screen of the Android

I am writing a app for which i need to display the Time elapsed when user presses a button in UI screen . So please guide me in writing the code in Android. I need to display the time in seconds, mins and hours.

Thanks in anticipation.

Upvotes: 2

Views: 5344

Answers (1)

whlk
whlk

Reputation: 15635

You can get the current system time in milliseconds by calling System.currentTimeMillis(). You can store this in an variable on Activity start and then calculate the difference between the current time and the stored time in the onClick method of the button.

example:

class ExampleApp exnteds Activity {
    private long startTime;
    public void onCreate(Bundle sis) {
        startTime = System.currentTimeMillis();
        ...

    public void onMyButtonClick(View v) {
        long timeGoneMillis = System.currentTimeMillis() - startTime;
        long timeGoneSecs = timeGoneMillis / 1000;
        ...

Upvotes: 4

Related Questions