Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chronometer with H:MM:SS

Tags:

java

android

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" />
like image 702
Rathi J Avatar asked Jul 07 '16 05:07

Rathi J


People also ask

How do I change the chronometer format?

setOnChronometerTickListener(new Chronometer. OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { CharSequence text = chronometer. getText(); if (text. length() == 5) { chronometer.

What is chronometer in Android?

android.widget.Chronometer. Class that implements a simple timer. You can give it a start time in the SystemClock#elapsedRealtime timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call start() .

How do you use the chronometer on Kotlin?

Access ChronoMeter in MainActivity. First, we declare a variable meter to access the Chronometer from the XML layout file. then, we access the button from the xml file and set setOnClickListener to start and stop the timer. val btn = findViewById<Button>(R. id.


1 Answers

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

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

like image 88
sushildlh Avatar answered Sep 30 '22 11:09

sushildlh