Chronometer with H: MM: SS

How to display a chronometer with H: MM: SS? I read MM: SS and H: MM: SS are displayed 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" />
+4
source share
2 answers

Divide the time by minute, hour and second using setOnChronometerTickListener.

use it ......

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 a globally long variable timeWhenStopped. maintain time.

private long timeWhenStopped = 0;

Start the listener ... get the time when stopped and start from there.

 start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chrono.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
            chrono.start();
        }
    }); 

.... .

stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timeWhenStopped = chrono.getBase() - SystemClock.elapsedRealtime();
            chrono.stop();

        }
    });

............

+6

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" />

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();
    }
}
0

All Articles