Reputation: 13
My Android Studio chronometer is just showing minutes and hours like this 0.0 and I wanted just seconds and minutes. How can I achieve this. My code for Java is like this.
final Chronometer chrono =
(Chronometer)findViewById(R.id.chronometer);
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
Upvotes: 0
Views: 300
Reputation: 163
Use this code snippet.
Chronometer timeElapsed = (Chronometer) findViewById(R.id.chronomete);
timeElapsed.setOnChronometerTickListener(new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer cArg) {
long time = SystemClock.elapsedRealtime() - cArg.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String hh = h < 10 ? "0"+h: h+"";
String mm = m < 10 ? "0"+m: m+"";
String ss = s < 10 ? "0"+s: s+"";
cArg.setText(hh+":"+mm+":"+ss);
}
});
timeElapsed.setBase(SystemClock.elapsedRealtime());
timeElapsed.start();
Customize it as you like.
Upvotes: 2