Step Counter App

How to make a Step Counter App with Kotlin

A blog post to help you make your own fitness app.If you have an Android phone or tablet, chances are you have at least one fitness application. Dozens of applications have been developed to keep track of the steps people take and even help them lose weight. Let's take a look at how you can build one of your own.


A step counter that you can use to keep track of your steps. That is the main goal of this application and that is why we are going to create it. The first thing is to know what a step counter is.

A step counter is an application that keeps track of the number of steps you take during a given period of time and can also be used as a pedometer. This particular application will count the number of steps and display it on a screen graphically with at least two lines displaying the number of steps since its creation.

Step Counter Android Application Implementation


Creating User Interface

The Step Counter Fitness App's user interface design can now begin. The user interface will consist of two TextViews, one for "Steps" and one for the user's current step count.

The primary process's code is written in Kotlin.


<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

        app:layout_constraintTop_toTopOf="parent" />

        app:layout_constraintTop_toBottomOf="@+id/stepsLbl" />

Source: Github

It's time to get started on the Main Activity's Kotlin code.

The Step Counter sensor, which is found on some Android devices, will be used to track the user's movement. The step counter sensor is required for our software to function.

In the onCreate method of MainActivity, get the SensorManager service. The onResume method then attempts to retrieve the Step Counter sensor instance from the Android device using the SensorManager instance's getDefaultSensor method.

If this call returns null, it means the device does not have a step counter sensor, and the user will be notified.

To subscribe to our MainActivity to receive updates on the user's steps, use the registerListener method of the previously obtained SensorManager instance. The MainActivity must follow the SensorEventListener specification to accomplish this.

Remember that a boolean is also used to indicate whether or not a user is active. We consider the user to be active when an application is launched into the foreground. If the user is not active, we will assume they are not running.

This is accomplished by subclassing SensorEventListener and overriding its onSensorChanged method in our MainActivity. This function is called when the step counter detects motion. The step count is obtained by reading the first index of the SensorEvent parameter's values array.

We can record the user's steps and display them in the TextView if they are running.

package com.ssaurel.stepcounter

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.widget.Toast

class MainActivity : AppCompatActivity(), SensorEventListener {

    var running = false
    var sensorManager:SensorManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

    override fun onResume() {
        running = true
        var stepsSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)

        if (stepsSensor == null) {
            Toast.makeText(this, "No Step Counter Sensor !", Toast.LENGTH_SHORT).show()
        } else {
            sensorManager?.registerListener(this, stepsSensor, SensorManager.SENSOR_DELAY_UI)

    override fun onPause() {
        running = false

    override fun onAccuracyChanged(p0: Sensor?, p1: Int) {

    override fun onSensorChanged(event: SensorEvent) {
        if (running) {
            stepsValue.setText("" + event.values[0])

Source: Github

Our fitness tracker app is being thoroughly tested.

It's time to put our Step Counter Fitness App to the test! Because the Android emulator cannot simulate the behavior of the Step Counter sensor, we must run our application on a real Android device. Obviously, a device with a step counter sensor is required for this to work.

Bottom Line

Finally, the development of a step counter fitness app in Kotlin will no longer be discussed. In the coming years, we intend to continue developing increasingly sophisticated applications. Please leave your comments in the section below.

Takeaway: You will know how to make a step counter with Android and Kotlin.

Leave a Reply

Your email address will not be published. Required fields are marked *