{"id":1230,"date":"2016-04-15T05:23:11","date_gmt":"2016-04-15T05:23:11","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1230"},"modified":"2019-06-12T06:55:00","modified_gmt":"2019-06-12T06:55:00","slug":"chronometer","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/chronometer","title":{"rendered":"Chronometer Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, Chronometer is a class that implements a simple timer. Chronometer is a\u00a0subclass of TextView. This class helps us to add a timer in our app.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1508\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/Chronometer-in-Android.jpg\" alt=\"Chronometer in Android\" width=\"236\" height=\"203\" \/><\/p>\n<p>You can give Timer\u00a0start time in the elapsedRealTime() timebase and it start counting from that. If we\u00a0don\u2019t give base time then it will use the time at which time\u00a0we\u00a0call start() method. By default a chronometer displays the current timer value in the form of MM:SS or H:MM:SS. We can set our own format into an arbitrary string.<\/p>\n<p><strong>Chronometer code\u00a0in XML:<\/strong><\/p>\n<pre>&lt;Chronometer\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\" \/&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Methods Of Chronometer In Android:<\/strong><\/h4>\n<p>Let\u2019s discuss some important methods of chronometer that may be called in order to manage the chronometer.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. start():<\/strong><\/span> start function of chronometer is used to start the counting up.<\/p>\n<p>Below we start the counting up of a chronometer.<\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nsimpleChronometer.start(); \/\/ start a chronometer<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1352\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/chronometer-start-android.gif\" alt=\"chronometer start android\" width=\"306\" height=\"188\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>2. stop():<\/strong><\/span> stop function of chronometer is used to stop the counting up.<\/p>\n<p>Below code stop the counting of a chronometer.<\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nsimpleChronometer.stop(); \/\/ stop a chronometer<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. setFormat(String format):<\/strong><\/span> set format function of chronometer is used to set the format string used to display. In other words we can say it is used to display text, numbers etc along-with chronometer.<\/p>\n<p>In the below example code we set the string format for displaying a chronometer.<\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\nsimpleChronometer.start(); \/\/ start a chronometer\r\nsimpleChronometer.setFormat(\"Time Running - %s\"); \/\/ set the format for a chronometer<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1353\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/Chronometer-setFormat-Android.gif\" alt=\"Chronometer setFormat Android\" width=\"306\" height=\"122\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>4. getformat():<\/strong><\/span>\u00a0This function of chronometer is used for getting the current format string. This methods returns a string type value.<\/p>\n<p>Below we get the current format string from a chronometer.<\/p>\n<pre>simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nString formatType=simpleChronometer.getFormat(); \/\/ get the format from a chronometer<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):<\/strong><\/span>\u00a0This\u00a0is a listener event which is automatically called when the chronometer changes.<\/p>\n<p>Below we show the use of setOnChronometerTickListener() for a chronometer.<\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\/\/ perform set on chronometer tick listener event of chronometer\r\n\r\nsimpleChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {\r\n\r\n@Override\r\n\r\npublic void onChronometerTick(Chronometer chronometer) {\r\n\r\n\/\/ do something when chronometer changes\r\n\r\n}\r\n\r\n});\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. setBase(long base):<\/strong><\/span> 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&#8217;t give it a base time, it will use the time at which you call start().<\/p>\n<p><code>SystemClock.elapsedRealtime()<\/code> is the number of milliseconds since the device was turned on.<\/p>\n<p>Below we set the base time for a chronometer.<\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nsimpleChronometer.setBase(SystemClock.elapsedRealtime()); \/\/ set base time for a chronometer\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. getBase():<\/strong><\/span> get base function is used to get the base time from a chronometer. This method returns a base time as set through the setBase() function. This method return long value.<\/p>\n<p>Below we get the base time from a chronometer.<\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nlong base=simpleChronometer.getBase(); \/\/ get base time from a chronometer\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of Chronometer In Android:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some common attributes of a chronomter that helps us to configure it in our layout (xml).<\/p>\n<p><strong><span style=\"color: #008000;\">1. Id:<\/span>\u00a0<\/strong>id attribute is used to uniquely identify a chronometer.<\/p>\n<pre>&lt;Chronometer\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\" \/&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. gravity:<\/strong><\/span> The gravity attribute is an optional attribute which is used to control the alignment of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.<\/p>\n<p>Below we set the center_horizontal gravity for text of a chronometer.<\/p>\n<pre>&lt;Chronometer\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:gravity=\"center_horizontal\" \/&gt;&lt;!-- center horizontal gravity for the text of chronometer --&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1342\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/gravity-in-Chronometer-Android.jpg\" alt=\"gravity in Chronometer Android\" width=\"256\" height=\"188\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>3. textColor:<\/strong><\/span> textColor attribute is used to set the text color of chronometer. Color value is in the form of &#8220;#argb&#8221;, &#8220;#rgb&#8221;, &#8220;#rrggbb&#8221;, or &#8220;#aarrggbb&#8221;.<\/p>\n<p>Below we set the red color for the displayed text of a chronometer.<\/p>\n<pre>&lt;Chronometer\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:gravity=\"center_horizontal\"\r\nandroid:textColor=\"#f00\"\/&gt;&lt;!-- red color for the text of chronometer --&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1345\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/textColor-in-Chronometer-Android.jpg\" alt=\"textColor in Chronometer Android\" width=\"303\" height=\"204\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/textColor-in-Chronometer-Android.jpg 303w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/textColor-in-Chronometer-Android-300x202.jpg 300w\" sizes=\"auto, (max-width: 303px) 100vw, 303px\" \/><\/p>\n<p><strong>Setting textColor of Chronometer In Java class:<\/strong><\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nsimpleChronometer.setTextColor(Color.RED); \/\/ set red color for the displayed text of a chronometer<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. textSize:<\/strong><\/span> textSize attribute is used to set the size of text of chronometer. We can set the text size in sp (scale independent pixel) or dp ( density pixel ).<\/p>\n<p>Below we set the 25sp size for the text of a chronometer.<\/p>\n<pre>&lt;Chronometer\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:gravity=\"center_horizontal\"\r\nandroid:textColor=\"#f00\"\r\nandroid:textSize=\"25sp\"\/&gt;&lt;!-- 25 sp size for displayed text of chronometer --&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1346\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/textSize-in-Chronometer-Android.jpg\" alt=\"textSize in Chronometer Android\" width=\"304\" height=\"228\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/textSize-in-Chronometer-Android.jpg 304w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/textSize-in-Chronometer-Android-300x225.jpg 300w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/p>\n<p><strong>Setting textSize of Chronometer In Java class:<\/strong><\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nsimpleChronometer.setTextSize(25); \/\/ set text size of a chronometer<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. textStyle:<\/strong><\/span> textStyle attribute is used to set the text style of a chronometer. The possible text styles are bold, italic and normal.\u00a0 If we need to use two or more styles for a chronomter text then \u201c|\u201d operator is used for that.<\/p>\n<p>Below we set the bold and italic text styles for text of a chronometer.<\/p>\n<pre>&lt;Chronometer\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:textColor=\"#f00\"\r\nandroid:textSize=\"25sp\"\r\nandroid:textStyle=\"bold|italic\"\/&gt;&lt;!-- bold and italic style for displayed text of chronometer --&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1347\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/textStyle-in-Chronometer-Android.jpg\" alt=\"textStyle in Chronometer Android\" width=\"304\" height=\"235\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/textStyle-in-Chronometer-Android.jpg 304w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/textStyle-in-Chronometer-Android-300x232.jpg 300w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>6. background:<\/strong><\/span> background attribute is used to set the background of a chronometer. You \u00a0can set a color or a drawable in the background of a text view. You can also set the background color programmatically means in java class.<\/p>\n<p>Below we set the green color for the background of a chronometer.<\/p>\n<pre>&lt;Chronometer\r\n\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"50dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:textColor=\"#f00\"\r\nandroid:textSize=\"25sp\"\r\nandroid:textStyle=\"bold|italic\"\r\nandroid:background=\"#0f0\"\/&gt;&lt;!-- green background color of chronometer --&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1348\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/background-in-Chronometer-Android.jpg\" alt=\"background in Chronometer Android\" width=\"308\" height=\"235\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/background-in-Chronometer-Android.jpg 308w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/background-in-Chronometer-Android-300x229.jpg 300w\" sizes=\"auto, (max-width: 308px) 100vw, 308px\" \/><\/p>\n<p><strong>Setting background of Chronometer In Java class:<\/strong><\/p>\n<pre>Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); \/\/ initiate a chronometer\r\n\r\nsimpleChronometer.setBackgroundColor(Color.GREEN); \/\/ set green color for the background of a chronometer<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. padding:<\/strong><\/span> padding attribute is used to set the padding from left, right, top or bottom.<\/p>\n<ul>\n<li><strong>paddingRight :\u00a0<\/strong>set the padding from the right side of the chronometer<strong>.<\/strong><\/li>\n<li><strong>paddingLeft :\u00a0<\/strong>set the padding from the left side of the chronometer<strong>.<\/strong><\/li>\n<li><strong>paddingTop :\u00a0<\/strong>set the padding from the top side of the chronometer<strong>.<\/strong><\/li>\n<li><strong>paddingBottom :\u00a0<\/strong>set the padding from the bottom side of the<\/li>\n<li><strong>Padding :\u00a0<\/strong>set the padding from the all side\u2019s of the chronometer<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set\u00a020dp padding from the top of a chronometer.<\/p>\n<pre>&lt;Chronometer\r\n\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"50dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:textColor=\"#f00\"\r\nandroid:textSize=\"25sp\"\r\nandroid:textStyle=\"bold|italic\"\r\nandroid:background=\"#0f0\"\r\nandroid:paddingTop=\"20dp\"\/&gt;&lt;!-- 20 dp padding from the top of a chronometer --&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1349\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/padding-in-Chronometer-Android.jpg\" alt=\"padding in Chronometer Android\" width=\"304\" height=\"244\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/padding-in-Chronometer-Android.jpg 304w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/padding-in-Chronometer-Android-300x241.jpg 300w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>8. drawableBottom:<\/strong><\/span>\u00a0drawableBottom is the drawable to be drawn to the below of the text of chronometer.<\/p>\n<p><span style=\"color: #008000;\"><strong>9. drawableTop:<\/strong><\/span>\u00a0drawableTop is the drawable to be drawn to the top of the text of a chronometer.<\/p>\n<p><span style=\"color: #008000;\"><strong>10. drawableRight:<\/strong><\/span>\u00a0drawableRight is the drawable to be drawn to the right side of the text of a chronometer.<\/p>\n<p><span style=\"color: #008000;\"><strong>11. drawableLeft:<\/strong><\/span>\u00a0drawableLeft is the drawable to be drawn to the left side of the text of a chronometer.<\/p>\n<p>Below we set the icon to the left side of the text of chronometer. Make sure to save ic_launcher image in drawable folder.<\/p>\n<pre>&lt;Chronometer\r\n\r\nandroid:id=\"@+id\/simpleChronometer\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:textColor=\"#f00\"\r\nandroid:textSize=\"25sp\"\r\nandroid:textStyle=\"bold|italic\"\r\nandroid:background=\"#0f0\"\r\nandroid:gravity=\"center\"\r\nandroid:padding=\"10dp\"\r\nandroid:drawableLeft=\"@drawable\/ic_launcher\"\/&gt;&lt;!-- drawable left of a chronometer --&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1350\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/drawable-in-Chronometer-Android.jpg\" alt=\"drawable in Chronometer Android\" width=\"302\" height=\"245\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/drawable-in-Chronometer-Android.jpg 302w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/drawable-in-Chronometer-Android-300x243.jpg 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/p>\n<hr \/>\n<h4><strong>Chronometer Example in Android Studio:<\/strong><\/h4>\n<p>In the\u00a0example of chronometer we display chronometer with five buttons and perform click events on that buttons. Buttons are used to start, stop, restart, setFormat and clearFormat of chronometer. Below is the final output, download project code and step by step explanation.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ChronometerExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1354\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/Chronometer-Example-Android.gif\" alt=\"Chronometer Example Android\" width=\"305\" height=\"418\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ChronometerExample.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt; activity_main.xml (or) main.xml and add following code:<\/p>\n<p>In this step we open xml file and add the code for displaying a chronometer with five buttons to perform operations on chronometer.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/textView1\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"@string\/chronometerInAndroid\"\r\n        android:textColor=\"#FF0000\"\r\n        android:textSize=\"20dp\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/startButton\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/textView1\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"@string\/start\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/stopButton\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/startButton\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"@string\/stop\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/restartButton\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/stopButton\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"@string\/restart\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/setFormat\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/restartButton\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"@string\/setFormat\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/clearFormat\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/setFormat\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"10dp\"\r\n        android:text=\"@string\/clearFormat\" \/&gt;\r\n    &lt;!-- chronometer with black background and red text color --&gt;\r\n    &lt;Chronometer\r\n        android:id=\"@+id\/simpleChronometer\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/clearFormat\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:background=\"#000\"\r\n        android:gravity=\"center\"\r\n        android:padding=\"10dp\"\r\n        android:textColor=\"#f00\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code to\u00a0initiate the chronometer, five button and then perform on click listener event on buttons to perform start, stop and other operations on chronometer as discussed earlier in this post.<\/p>\n<pre>package example.abhiandroid.chronometerexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.os.SystemClock;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.Chronometer;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    Chronometer simpleChronometer;\r\n    Button start, stop, restart, setFormat, clearFormat;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ initiate views\r\n        simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer);\r\n        start = (Button) findViewById(R.id.startButton);\r\n        stop = (Button) findViewById(R.id.stopButton);\r\n        restart = (Button) findViewById(R.id.restartButton);\r\n        setFormat = (Button) findViewById(R.id.setFormat);\r\n        clearFormat = (Button) findViewById(R.id.clearFormat);\r\n        \/\/ perform click  event on start button to start a chronometer\r\n        start.setOnClickListener(new View.OnClickListener() {\r\n\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ TODO Auto-generated method stub\r\n\r\n                simpleChronometer.start();\r\n            }\r\n        });\r\n\r\n        \/\/ perform click  event on stop button to stop the chronometer\r\n        stop.setOnClickListener(new View.OnClickListener() {\r\n\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ TODO Auto-generated method stub\r\n\r\n                simpleChronometer.stop();\r\n            }\r\n        });\r\n\r\n        \/\/ perform click  event on restart button to set the base time on chronometer\r\n        restart.setOnClickListener(new View.OnClickListener() {\r\n\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ TODO Auto-generated method stub\r\n\r\n                simpleChronometer.setBase(SystemClock.elapsedRealtime());\r\n            }\r\n        });\r\n\r\n        \/\/ perform click  event on set Format button to set the format of chronometer\r\n        setFormat.setOnClickListener(new View.OnClickListener() {\r\n\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ TODO Auto-generated method stub\r\n\r\n                simpleChronometer.setFormat(\"Time (%s)\");\r\n            }\r\n        });\r\n\r\n        \/\/ perform click  event on clear button to clear the current format of chronmeter as you set through set format\r\n        clearFormat.setOnClickListener(new View.OnClickListener() {\r\n\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ TODO Auto-generated method stub\r\n\r\n                simpleChronometer.setFormat(null);\r\n            }\r\n        });\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Open res -&gt; values -&gt; strings.xml<\/p>\n<p>In this step we shows the list of strings that will be used in our example.<\/p>\n<pre>&lt;resources&gt;\r\n    &lt;string name=\"app_name\"&gt;ChronometerExample&lt;\/string&gt;\r\n    &lt;string name=\"hello_world\"&gt;Hello world!&lt;\/string&gt;\r\n    &lt;string name=\"action_settings\"&gt;Settings&lt;\/string&gt;\r\n    &lt;string name=\"chronometerInAndroid\"&gt;Chronometer In Android&lt;\/string&gt;\r\n    &lt;string name=\"start\"&gt;Start&lt;\/string&gt;\r\n    &lt;string name=\"stop\"&gt;Stop&lt;\/string&gt;\r\n    &lt;string name=\"restart\"&gt;Restart&lt;\/string&gt;\r\n    &lt;string name=\"setFormat\"&gt;Set Format&lt;\/string&gt;\r\n    &lt;string name=\"clearFormat\"&gt;Clear Format&lt;\/string&gt;\r\n&lt;\/resources&gt;<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and you will see 5 buttons to start, stop, restart,\u00a0set format and clear format of Chronometer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, Chronometer is a class that implements a simple timer. Chronometer is a\u00a0subclass of TextView. This class helps us to add a timer in our app. You can give Timer\u00a0start time in the elapsedRealTime() timebase and it start counting from that. If we\u00a0don\u2019t give base time then it will use the time at which &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/chronometer\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Chronometer Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1230","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Chronometer Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn Chronometer, its methods and attributes with step by step explanation using example in Android Studio. In Android, Chronometer is a class that implements a simple timer.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/chronometer\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1230"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1230\/revisions"}],"predecessor-version":[{"id":2781,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1230\/revisions\/2781"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}