Rathi J Rathi J - 5 months ago 28
Android Question

Chronometer with H:MM:SS

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 button.

public class MainActivity extends AppCompatActivity {

Button start,stop;
Chronometer chrono;
long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
chrono = (Chronometer) findViewById(R.id.chronometer);

start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

chrono.setBase(SystemClock.elapsedRealtime());


chrono.start();
}
});

stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chrono.stop();

}
});


}

}


here is my xml code for MainActivity:

<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/start"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/stop"
android:layout_below="@+id/start"
android:layout_centerHorizontal="true" />

Answer

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");

output

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............