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 create a Chronometer Android app, follow the steps given below.
Step 1
Now, open Android Studio and choose the File and New. Afterwards, choose NewProject.
Step 2
Here, you can create your Application name and choose where your project should be stored on the location and click 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 Next button.
Now, we can write the activity name and click Finish button.
Step 4
Now, open your project and you will go to activity_main.xml. Afterwards, you will build the design, which should be the toolbox, and if you want, some option 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
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="xyz.rvconstructions.www.chronometerapp.MainActivity">
-
-
- <Chronometer
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/chronometer"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:background="#000000"
- android:gravity="center"
- android:padding="10dp"
- android:textColor="#1aff00"
- android:textStyle="bold"
- android:layout_marginTop="102dp" />
-
- <Button
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:text="START"
- android:id="@+id/strbutton"
- android:layout_below="@+id/chronometer"
- android:layout_marginTop="10dp"
- android:layout_centerHorizontal="true" />
-
- <Button
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:text="STOP"
- android:id="@+id/stpbutton"
- android:layout_below="@+id/strbutton"
- android:layout_marginTop="10dp"
- android:layout_centerHorizontal="true" />
-
- <Button
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:text="RESTART"
- android:id="@+id/rsbutton"
- android:layout_below="@+id/stpbutton"
- android:layout_marginTop="10dp"
- android:layout_centerHorizontal="true" />
-
- <Button
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:text="SETFORMAT"
- android:id="@+id/sfbutton"
- android:layout_below="@+id/rsbutton"
- android:layout_marginTop="10dp"
- android:layout_centerHorizontal="true" />
-
- <Button
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:text="CLEARFORMAT"
- android:id="@+id/clrbutton"
- android:layout_below="@+id/sfbutton"
- android:layout_marginTop="10dp"
- android:layout_centerHorizontal="true" />
-
-
- </RelativeLayout>
Step 6
Now, you will go to MainActivity.java page and build Java code.First, you need to declare a file, which is an extension file.
Now, we can see MainActivity.java code.
- package xyz.rvconstructions.www.chronometerapp;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Chronometer;
-
- public class MainActivity extends AppCompatActivity {
-
- Chronometer FirstChronometer;
- Button start, stop, restart, setFormat, clearFormat;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- FirstChronometer = (Chronometer) findViewById(R.id.chronometer);
- start = (Button) findViewById(R.id.strbutton);
- stop = (Button) findViewById(R.id.stpbutton);
- restart = (Button) findViewById(R.id.rsbutton);
- setFormat = (Button) findViewById(R.id.sfbutton);
- clearFormat = (Button) findViewById(R.id.clrbutton);
-
- start.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
- FirstChronometer.start();
- }
- });
-
-
- stop.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- FirstChronometer.stop();
- }
- });
-
-
- restart.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
- FirstChronometer.setBase(SystemClock.elapsedRealtime());
- }
- });
-
-
- setFormat.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
- FirstChronometer.setFormat("Time (%s)");
- }
- });
-
-
- clearFormat.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- FirstChronometer.setFormat(null);
- }
- });
-
- }
-
- }
Step 7
Here, you will go to run and select run app option.
Here, you will choose the Emulator or the devices, which is like 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 Stop button.
Now, you will click Restart button.
Now, you will click Setformat button.
Now, you will click ClearFormat button.