How to display Chronometer with H:MM:SS? I read MM:SS and H:MM:SS are displaying by default.I found only for MM:SS.
Here is my code for MM:SS with start and stop but
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_data"
android:layout_centerInParent="true" />
In activity
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tvData;
Chronometer chronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvData = (TextView) findViewById(R.id.tv_data);
chronometer = (Chronometer) findViewById(R.id.chronometer);
startChronometer();
chronometer.setOnChronometerTickListener(new OnChronometerTickListener() {
public void onChronometerTick(Chronometer cArg) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, getCurrentMiliSecondsOfChronometer());
tvData.setText(DateFormat.format("HH:mm:ss", calendar.getTime()));
}
});
}
private int getCurrentMiliSecondsOfChronometer() {
int stoppedMilliseconds = 0;
String chronoText = chronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000 + Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds =
Integer.parseInt(array[0]) * 60 * 60 * 1000 + Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}
return stoppedMilliseconds;
}
private void startChronometer() {
int stoppedMilliseconds = getCurrentMiliSecondsOfChronometer();
chronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
chronometer.start();
}
}
Chronometer with H:MM:SS
Divide the time into 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 maintain the 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();
}
});
enjoy coding............
You can also set with attractive Chronometer format like 00 hrs : 00 min : 00 sec
chronometer.setText("00 hrs : 00 min : 00 sec");
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
CharSequence text = chronometer.getText();
if (text.length() == 5) {
String a[] = text.toString().split(":");
String min = a[0] + " min : ";
String sec = a[1] + " sec";
chronometer.setText("00 hrs : " + min + sec);
} else if (text.length() == 7) {
String a[] = text.toString().split(":");
String hr = a[0] + " hrs : ";
String min = a[1] + " min : ";
String sec = a[2] + " sec";
chronometer.setText("0" + hr + min + sec);
}
}
});