Chronometer Example

The Chronometer is used for the time-based application. If you are targeting to create an application that has some functionality to do with the timer-based task then it is perfect for you.

The chronometer has different methods which make it very easy to use. Some of are : 

1) start - To start the timer.
2) stop - To stop the timer.
3) reset - To reset the timer.
4) resume - To resume the timer.

Let's try with an example. This example will show you how can you work with a chronometer in the android application.

The example includes how to bind the chronometer in the project and used the different methods provided by it.  

First, let's add the chronometer. To add it please copy the below piece of code in your XML file.

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>

    <Chronometer        
       android:id="@+id/chronometer"
       android:textSize="20sp"/>

    <Button
        android:text="Start"
        android:id="@+id/btn_start"/>

    <Button
        android:text="Stop"
        android:id="@+id/btn_stop"/>

    <Button
        android:text="reset"
        android:id="@+id/btn_reset"/>

</RelativeLayout>

We have added it so let's bind it and perform some operation on it. To do that we have to add some logic in the java file. Let's do some coding!

Main.java

public class MainActivity extends AppCompatActivity {


    Chronometer chronometer;
    Button start,stop,reset;
    long timeStopped=0;
    @Override    

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        chronometer = (Chronometer) findViewById(R.id.chronometer);
        start= (Button) findViewById(R.id.btn_start);
        stop= (Button) findViewById(R.id.btn_stop);
        reset= (Button) findViewById(R.id.btn_reset);

        start.setOnClickListener(new View.OnClickListener() {
            @Override            
            public void onClick(View v) {
                chronometer.setBase(SystemClock.elapsedRealtime()+timeStopped);
                chronometer.start();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override            
             public void onClick(View v) {
                timeStopped=chronometer.getBase()-SystemClock.elapsedRealtime();
                chronometer.stop();
                start.setText("Resume");
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {
            @Override           
             public void onClick(View v) {
               timeStopped=0;
                chronometer.stop();
                chronometer.setText("00:00");
                start.setText("Start");
            }
        });
    }
}

To start the time first you have to set the base of the timer so that we know from which time the timer needs to start. You can set the time you want. Once the base is set you can perform the start() method to run the timer.

Once the timer is start you can use different methods to stop, reset and resume the timer that we have seen in the above example.

In the above example, we have taken the timeStopped object which will hold the current time to make them stop and resume functionality work properly.

Output

         


Comments