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.
Asked
Active
Viewed 3.6k times
1 Answers
17
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.

user1014917
- 681
- 7
- 13
-
I have more layouts (XMLs) that I want to use the chronometer with... How do I adapt this line "setContentView(R.layout.test);" to enable it to receive calls from other layouts, not just from the "test" one? – user1123530 Dec 30 '11 at 21:38
-
I don't know whether I get you right, but you will need a Chronometer in every XML-layout if you don't add the Chronometer in Java. – user1014917 Dec 30 '11 at 21:49
-
Well, I have a menu which redirects me to one of five identic XMLs with a chronometer and a java file, both java and XMLs with the code above... How do I adapt the java file to serve five xmls? Or do I have to create a java file for every xml? – user1123530 Dec 30 '11 at 22:17
-
If the XML is always identic you can use it for every of your menupoints. If the Java is the same, sure use the same Java. Just start the same Activity with your menupoints. But I don't see the point! – user1014917 Dec 30 '11 at 22:25
-
Well, the XMLs are identic for now, i want to add different text to each other later... – user1123530 Dec 30 '11 at 22:42
-
I would recommend using one single XML file. Just get your View via findViewById() and alter the text, if the structure is identic. – user1014917 Dec 30 '11 at 22:44
-
That sounds good, but how can I transfer the choice from the menu (an integer, ex. position 1,2,3...) to the XML so that I can use findViewByld()? I'm using a java file for the menu: [link](http://dl.dropbox.com/u/51147339/Menu.java) – user1123530 Dec 30 '11 at 23:29
-
I would add the information via an Intent. Just create a new intent, add the Integer and hand it with the startActivity(). I think you hardly lack basics. I would recommend you to read some tutorials, the Android Developers Page and some Android developing books first. – user1014917 Dec 30 '11 at 23:39