Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a chronometer in Android

I'd like to know how can I implement in Android a simple chronometer with a start and stop button that displays data in the HH:MM:SS:MsMs format... I've been searching and searching and I have found some classes on google developer, but they didn't give examples and I got lost... Could you direct me to a tutorial/example? I'm just starting out in Android :) Any help would be much appreciated.

like image 932
user1123530 Avatar asked Dec 30 '11 21:12

user1123530


People also ask

How to set time in chronometer in Android?

setBase(long base): set base function of chronometer is used to set the time that count up time is in reference to. You can give it a start time in the 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(). SystemClock.

What is chronometer in Android?

Chronometer is a widget in android which is used to display a timer-like view in the android application. We can use it like a timer where we can provide an up and down time counter. In this article, we will look at how to use Chronometer in an android application.

How do you use the chronometer on Kotlin?

Create ChronoMeter in MainActivity. First, we declare a variable meter to create the Chronometer in Kotlin 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

Just implement the Chronometer in XML or Code and use its start() method to start it and its stop() method to stop it.

More can be found here: http://developer.android.com/reference/android/widget/Chronometer.html

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" 
        android:onClick="startChronometer"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" 
        android:onClick="stopChronometer"/>

</LinearLayout>

Java:

public class Main extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    }

    public void startChronometer(View view) {
        ((Chronometer) findViewById(R.id.chronometer1)).start();
    }

    public void stopChronometer(View view) {
        ((Chronometer) findViewById(R.id.chronometer1)).stop();
    }
}

You might add some code to the startChronometer() method to restart the counter.

like image 117
user1014917 Avatar answered Sep 29 '22 00:09

user1014917