Android smartphones and tablets have a lot of sensors letting developers to create great applications answering to various use cases. In that tutorial, you are going to discover how to create a Step Counter Fitness Application for Android with Kotlin. A good way to discover how to make a simple application in Kotlin on Android Studio.

You can also discover this tutorial on the SSaurel’s Channel on YouTube :

First step is to create a new Android Studio Project with Kotlin support enabled. We assume that you know how to create this project. If you want to discover how to create this first Kotlin project on Android Studio, you can read the following tutorial on the SSaurel’s Blog : https://www.ssaurel.com/blog/create-your-first-android-app-with-kotlin/

 

Creating the User Interface

Now, we can create the User Interface of our Step Counter Fitness App. The User Interface will be quite simple with two TextView : one to display “Steps” and another to display the number of steps of the user.

The layout will have the following form :

 

Writing the Kotlin Code of the Main Activity

Now, it’s time to write the Kotlin Code of the Main Activity. In order to measure the steps of the user, we will use the Step Counter sensor available on some Android devices. Obviously, our application will work only on devices having a Step Counter sensor.

To start, we will need to get the SensorManager service in the onCreatemethod of the MainActivity. Then, in the onResume method, we try to get an instance of the Step Counter sensor of the Android device by calling the getDefaultSensor method of the SensorManager instance.

If this call returns null, the device has no Step Counter sensor and we display a message alerting the user. Otherwise, we can register our MainActivity to receive the updates of the user’s steps by calling the registerListener method of the SensorManager instance got previously. For that, the MainActivity needs to implement the SensorEventListener interface.

Note that we also use a boolean indicating if the user is running or no. For that, we simply consider that a user is running when the application is launched in the foreground. Otherwise, we consider the user is not running.

By implementing the SensorEventListener interface, our MainActivity overrides the onSensorChanged method. This method will be called when the Step Counter sensor detects some steps. By reading the first index of the values array of the SensorEvent passed in parameter, we can get the number of steps of the user.

If the user is running, we update the TextView letting us to display the number of steps walked by the user. This gives us the following code for our MainActivity :

 

Testing our Step Counter Fitness App

The time has come to test our Step Counter Fitness App. Like the Android emulator don’t let us to simulate the behavior of the Step Counter sensor, we need to execute our application on a real Android device. Obviously, you need to execute on a device having a Step Counter sensor.

Once launched, the Step Counter Fitness App we have made with Kotlin should look like that :

This tutorial learning you how to create a Step Counter Fitness App with Kotlin is ended. In the future, we will continue by creating more complex Applications. Don’t hesitate to give me your ideas in comments.

To discover more tutorials on Android Development, you can also visit the SSaurel’s Channel on YouTube :