Android ChronoMeter

ChronoMeter can be defined as below –

"ChronoMeter is a class that uses a simple android timer. This class helps us to add a timer to our app. You can give it a start time and it will start counting up from there, or if you don’t provide a base time, it will use the time at which you will call start() method."

Some of the popular attributes of android ChronoMeter  are –

 

 

S. No. XML Attributes Description
1 android: id  
2 android: height  
3 android: width  
4 android: alpha Specifies alpha of the view.
5 android: background Specifies drawable of the background of the view.
6 android: elevation Specifies elevation of the view.
7 android: padding Specifies padding to apply the edges of the view.
8 android: visibility Specifies visibility(VISIBLE, INVISIBLE or GONE) of the view.
9 android: gravity Specifies gravity of the view.
10 android: countDown Specifies whether this chronometer will count up or count down from the base value.
11 android: format Specifies the format of the string to be shown.
12 android: maxHeight Specifies maximum height of the view.
13 android: maxLength Set an input filter to constrain the text length to the specified number.
14 android: maxWidth Specifies maximum width of the view.
15 android: minHeight Specifies minimum height of the view.
16 android: minWidth Specifies minimum width of the view.

At first, we will create android application. Then, we will use ChronoMeter in the application.

Follow steps below to create new project. Please ignore the steps if you've already created a new application.

 

 

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project Write application name as Chronometer. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button.
4 Then, select Empty Activity => click next => click finish.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial  to Create a New Project to know steps in detail.

Now, we will modify xml and java file to use chronoMeter in the application.

Open res/values/strings.xml file. Then, add below code into it.

<resources>
<string name="app_name">ChronoMeter</string>
<string name="stop">Stop</string>
<string name="start">Start</string>
<string name="playing">Playing...</string>
<string name="stopped">Stopped...</string>
</resources> 

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/chronometer"
android:paddingRight="20dp"
android:text="@string/start"/>

</android.support.constraint.ConstraintLayout>

In activity_main.xml file, we've defined Chronometer and Button. Now, we will access these widgets in java file to perform some operations on it. Access Chronometer in java file

Open src/main/java/com.ukacademe.chronometer/MainActivity.java file. Then, add below code into it.

package com.ukacademe.chronometer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Chronometer chronometer = findViewById(R.id.chronometer);

       final Button button = findViewById(R.id.button);
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() {

                boolean isPlaying = false;

                @Override
                public void onClick(View v) {
                    if (!isPlaying) {
                        chronometer.start();
                        isPlaying = true;
                    } else {
                        chronometer.stop();
                        isPlaying = false;
                    }

                    button.setText(isPlaying ? R.string.start : R.string.stop);
                    Toast.makeText(MainActivity.this, getString(isPlaying ? R.string.playing : R.string.stopped), Toast.LENGTH_SHORT).show();
                }
            });
        }

    }
}

 

 

In MainActivity.java file, we have accessed Chronometer and Button . Then, we have set a click listener to stop/start chronometer. A toast message will also be shown that represents the whether chronometer is playing or stopped.

Since the file of AndroidManifest.xml is very important in any android application, we are also going to see the content inside this file.

Code inside src/main/AndroidManifest.xml file is as below –

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

When we run the program, we will get output as shown below.