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. Darshan
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. Darshan
Here is a great example http://developer.android.com/resources/articles/timed-ui-updates.html
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;
...