Timer stopwatch based on Java Calendar class Android example

Start Stop Stopwatch Timer Android example source code.

private static long mStartTime = 0L;
Calendar cal;
TextView hTextViewVypis = (TextView)findViewById(R.id.idTextVypis);
 
void start(){
            cal = Calendar.getInstance();
            mStartTime = cal.getTimeInMillis();
}
 
void stop(){
     prinOutStopWatchTime();
}
 
 
    private void prinOutStopWatchTime() {
    	final long start = mStartTime;
 
    	cal = Calendar.getInstance();
    	long stopTime = cal.getTimeInMillis();
    	long millis = stopTime - start;
    	long milisekundy = millis % 1000;
    	int seconds = (int) (millis / 1000);
    	int minutes = seconds / 60;
    	seconds     = seconds % 60;
    	int hour = minutes / 60;
    	hour = hour % 60;
 
    	if (seconds < 10) {
    		hTextViewVypis.setText(hour +  ":" + minutes + ":0" + seconds + ":" + milisekundy);
    	} else {
    		hTextViewVypis.setText(hour  +  ":" + minutes + ":" + seconds + ":" + milisekundy);            
    	}
 
    }
This entry was posted in Android Examples Code and tagged , , , , , . Bookmark the permalink.


Comments are closed.

9,059 views