Create A Chronometer In Android App Using Android Studio

Introduction

 
In this article, you will learn how to develop a Chronometer Android application, using Android Studio.
 
Requirements
  • Android Studio version 2.1.3 
If you want to create a Chronometer Android app, follow the steps given below.
 
Step 1
 
Now, open Android Studio and choose the File and New. Afterward, choose new project.
 
 
Step 2
 
Here, you can create your Application name and choose where your project should be stored on the location and click the NEXT button.
 
 
Now, we can select the version of Android which is like Target Android Devices.
 
 
Step 3
 
Here, we can Add the activity and click the Next button.
 
 
Now, we can write the activity name and click the Finish button.
 
 
Step 4
 
Now, open your project and you will go to activity_main.xml. Afterward, you will build the design, which should be the toolbox, and if you want, some options like (Chronometer, button) with the help of the drag and drop method.
 
 
Now, we can see the Graphical User Interface design.
 
 
Step 5
 
Here, you build upon the design and write the.XML code.
 
Activity_mai.xml code 
  1.     <?xml version="1.0" encoding="utf-8"?>    
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     xmlns:tools="http://schemas.android.com/tools"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent"    
  6.     android:paddingBottom="@dimen/activity_vertical_margin"    
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"    
  8.     android:paddingRight="@dimen/activity_horizontal_margin"    
  9.     android:paddingTop="@dimen/activity_vertical_margin"    
  10.     tools:context="xyz.rvconstructions.www.chronometerapp.MainActivity">    
  11.         
  12.     <Chronometer    
  13.         android:layout_width="wrap_content"    
  14.         android:layout_height="wrap_content"    
  15.         android:id="@+id/chronometer"    
  16.         android:layout_alignParentTop="true"    
  17.         android:layout_centerHorizontal="true"    
  18.         android:background="#000000"    
  19.         android:gravity="center"    
  20.         android:padding="10dp"    
  21.         android:textColor="#1aff00"    
  22.         android:textStyle="bold"    
  23.         android:layout_marginTop="102dp" />    
  24.         
  25.     <Button    
  26.         android:layout_width="200dp"    
  27.         android:layout_height="wrap_content"    
  28.         android:text="START"    
  29.         android:id="@+id/strbutton"    
  30.         android:layout_below="@+id/chronometer"    
  31.         android:layout_marginTop="10dp"    
  32.         android:layout_centerHorizontal="true" />    
  33.         
  34.     <Button    
  35.         android:layout_width="200dp"    
  36.         android:layout_height="wrap_content"    
  37.         android:text="STOP"    
  38.         android:id="@+id/stpbutton"    
  39.         android:layout_below="@+id/strbutton"    
  40.         android:layout_marginTop="10dp"    
  41.         android:layout_centerHorizontal="true" />    
  42.         
  43.     <Button    
  44.         android:layout_width="200dp"    
  45.         android:layout_height="wrap_content"    
  46.         android:text="RESTART"    
  47.         android:id="@+id/rsbutton"    
  48.         android:layout_below="@+id/stpbutton"    
  49.         android:layout_marginTop="10dp"    
  50.         android:layout_centerHorizontal="true" />    
  51.         
  52.     <Button    
  53.         android:layout_width="200dp"    
  54.         android:layout_height="wrap_content"    
  55.         android:text="SETFORMAT"    
  56.         android:id="@+id/sfbutton"    
  57.         android:layout_below="@+id/rsbutton"    
  58.         android:layout_marginTop="10dp"    
  59.         android:layout_centerHorizontal="true" />    
  60.         
  61.     <Button    
  62.         android:layout_width="200dp"    
  63.         android:layout_height="wrap_content"    
  64.         android:text="CLEARFORMAT"    
  65.         android:id="@+id/clrbutton"    
  66.         android:layout_below="@+id/sfbutton"    
  67.         android:layout_marginTop="10dp"    
  68.         android:layout_centerHorizontal="true" />    
  69.         
  70. </RelativeLayout>   
    Step 6
     
    Now, you will go to the MainActivity.java page and build Java code.
     
    First, you need to declare a file, which is an extension file.
     
     
    Now, we can see the MainActivity.java code. 
    1. package xyz.rvconstructions.www.chronometerapp;  
    2. import android.support.v7.app.AppCompatActivity;  
    3. import android.os.Bundle;  
    4. import android.os.SystemClock;  
    5. import android.view.Menu;  
    6. import android.view.MenuItem;  
    7. import android.view.View;  
    8. import android.widget.Button;  
    9. import android.widget.Chronometer;  
    10. public class MainActivity extends AppCompatActivity {  
    11.  Chronometer FirstChronometer;  
    12.  Button start, stop, restart, setFormat, clearFormat;  
    13.  @Override  
    14.  protected void onCreate(Bundle savedInstanceState) {  
    15.   super.onCreate(savedInstanceState);  
    16.   setContentView(R.layout.activity_main);  
    17.   FirstChronometer = (Chronometer) findViewById(R.id.chronometer);  
    18.   start = (Button) findViewById(R.id.strbutton);  
    19.   stop = (Button) findViewById(R.id.stpbutton);  
    20.   restart = (Button) findViewById(R.id.rsbutton);  
    21.   setFormat = (Button) findViewById(R.id.sfbutton);  
    22.   clearFormat = (Button) findViewById(R.id.clrbutton);  
    23.   start.setOnClickListener(new View.OnClickListener() {  
    24.    @Override  
    25.    public void onClick(View v) {  
    26.     FirstChronometer.start();  
    27.    }  
    28.   });  
    29.   stop.setOnClickListener(new View.OnClickListener() {  
    30.    @Override  
    31.    public void onClick(View v) {  
    32.     FirstChronometer.stop();  
    33.    }  
    34.   });  
    35.   restart.setOnClickListener(new View.OnClickListener() {  
    36.    @Override  
    37.    public void onClick(View v) {  
    38.     FirstChronometer.setBase(SystemClock.elapsedRealtime());  
    39.    }  
    40.   });  
    41.   setFormat.setOnClickListener(new View.OnClickListener() {  
    42.    @Override  
    43.    public void onClick(View v) {  
    44.     FirstChronometer.setFormat("Time (%s)");  
    45.    }  
    46.   });  
    47.   clearFormat.setOnClickListener(new View.OnClickListener() {  
    48.    @Override  
    49.    public void onClick(View v) {  
    50.     FirstChronometer.setFormat(null);  
    51.    }  
    52.   });  
    53.  }  

      Step 7
       
      Here, you will go to run and select the run app option.
       
       
      Here, you will choose the Emulator or the devices, which are like the Nokia Nokia _X.
       
       
      Step 8
       
      Here, you can see the output, as shown below.
       
       
      Now, you will click the Start button.
       
       
      Now, you will click the Stop button.
       
       
      Now, you will click the Restart button.
       
       
      Now, you will click Setformat button.
       
       
      Now, you will click ClearFormat button.
       


      Similar Articles