Divide the time into the minute , hour and second using setOnChronometerTickListener.
use this ......
Chronometer chrono = (Chronometer) findViewById(R.id.chronomete);
chrono.setOnChronometerTickListener(new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer chronometer) {
long time = SystemClock.elapsedRealtime() - chronometer.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
chronometer.setText(t);
}
});
chrono.setBase(SystemClock.elapsedRealtime());
chrono.setText("00:00:00");
EDIT
For Start
Declare globally a long variable
timeWhenStopped
. It is to maintain time.private long timeWhenStopped = 0;
Start Listener... get the 'timeWhenStopped' and start from there.
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chrono.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
chrono.start();
}
});
Stop Listener.... store the time in timeWhenStopped and stop.
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timeWhenStopped = chrono.getBase() - SystemClock.elapsedRealtime();
chrono.stop();
}
});
That is all. If any help related to this post please comment.
Thank you, guys.
Enjoy coding.
No comments:
Post a Comment