Android ChronoMeter Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android chronoMeter widget in any android application. We will also go through different attributes of chronoMeter widget that can be used to customise it.

Output

Tutorialwing Android ChronoMeter Output Android ChronoMeter tutorial with example

Tutorialwing Android ChronoMeter Output

Getting Started

ChronoMeter widget can be defined as below –

ChronoMeter is a widget that are used as simple timer. 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. The timer also counts down from a base time. If you want to count down, you need to call setCountDown(boolean) method with true value.

Attributes of Android ChronoMeter Widget

Some of the popular attributes of android ChronoMeter widget are –

Sr. XML Attributes Description
1 android:countDown Specifies whether this chronometer will count up or count down from the base value
2 android:format Specifies the format of the string to be shown

Some of the popular attributes of android chronoMeter inherited from TextView are –

Sr. XML Attributes Description
1 android:gravity Specifies gravity of the view
2 android:height Specifies height of the view
3 android:maxHeight Specifies maximum height of the view
4 android:maxLength Set an input filter to constrain the text length to the specified number
5 android:maxWidth Specifies maximum width of the view
6 android:minHeight Specifies minimum height of the view
7 android:minWidth Specifies minimum width of the view
8 android:width Specifies width of the view

Some of the popular attributes of android chronoMeter inherited from View are –

Sr. XML Attributes Description
1 android:alpha Specifies alpha of the view
2 android:background Specifies drawable of the background of the view
3 android:elevation Specifies elevation of the view
4 android:id Specifies id of the view
5 android:padding Specifies padding to apply the edges of the view
6 android:visibility Specifies visibility(VISIBLE, INVISIBLE or GONE) of the view



Example of Android ChronoMeter Widget

At first, we will create android application. Then, we will use chronoMeter widget in this application.

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ChronoMeter. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 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 post to create a new project to know steps in detail.

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

2. Modify Values folder

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>

3. Use ChronoMeter Widget in xml file

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical"
	android:padding="20dp">

	<Chronometer
		android:id="@+id/chronometer"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="20dp"/>

	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:paddingLeft="20dp"
		android:paddingRight="20dp"
		android:text="@string/start"/>

</LinearLayout>

In activity_main.xml file, we have defined chronoMeter and button widgets. Now, we will access these widgets in java file to perform some operations on it.

4. Access ChronoMeter Widget in java file

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

package com.tutorialwing.chronometer;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.kt file, we have accessed chronoMeter and button widgets. 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 AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml

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

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

	<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 above.

That’s end of tutorial on Android ChronoMeter Widget.

Leave a Reply