Friday, 7 March 2014

How to Create and Implement Chronometer in Android Application

In this blog you can learn how to create and implement chronometer in any android apps.To create it you have to drag Chronometer in main.xml graphical editor from palette-->Time & Date folder.
 Only there is click on start button to start chronometer functionality also can include functionality stop, restart, set format and clear format .


main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical" >

  

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chronometer"
        android:layout_gravity="center"
        android:textColor="#336633"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:textStyle="bold"/>
<Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:text="Start" />
   
    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:text="Stop" />
   
     <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:text="Restart" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:text="Set Format" />

    <Button
        android:id="@+id/button5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:text="Clear Format" />
   

    <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"
       
        android:text="Chronometer" />

  
</LinearLayout>

ChronometerActivity.java

package com.rakesh.tiwari.chronometer;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class ChronometerActivity extends Activity {
    Chronometer cmeter;
    Button start, stop, restart, setformate, clearformate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cmeter = (Chronometer) findViewById(R.id.chronometer1);
        start = (Button) findViewById(R.id.button1);
        stop = (Button) findViewById(R.id.button2);
        restart = (Button) findViewById(R.id.button3);
        setformate = (Button) findViewById(R.id.button4);
        clearformate = (Button) findViewById(R.id.button5);
        start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cmeter.start();
            }
        });

        stop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cmeter.stop();
            }
        });

        restart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cmeter.setBase(SystemClock.elapsedRealtime());
                cmeter.start();
            }
        });

        setformate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cmeter.setFormat("Formate Time  - %s");
            }
        });

        clearformate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cmeter.setFormat(null);
            }
        });
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.chronometer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.chronometer.ChronometerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>






Chronometer

Chronometer Start

Chronometer Stop

Chronometer Set Formate


Chronometer Clear Format

Restart Chronometer