This weekend I thought I’d carry on with my valiant mission to conquer Kotlin and all that comes with it, today I have created my first Android app using Kotlin.
If you haven’t read my previous blogs then let me just fill you in a bit. I’m currently learning Kotlin, I have found the language a breath of fresh air, and I intend on carrying on with my endeavours.
Create a Kotlin app in 10 minutes
This blog isn’t going to be a tutorial on how to create an Android app using Kotlin, there are far more informed resources out there for that sort of thing.
I started the journey by going through a couple of written tutorials by google including how to create an interactive dice roller app and create a birthday card app. Both of which were very helpful in teaching me the ecosystem, and the dos and the don’ts of android development.
One thing I always recommend when following a tutorial is to do a little bit more than the tutorial asks. So if you — as I did — have to create an app that throws dice, create an app that throws dice and display the sum of the dice on the screen. You’ll notice this at the bottom of my tutorial blogs always go one step further, it really helps you to understand what’s going on, else, you’re just, copying.
Coding my first Android App with Kotlin
When creating an app, always make sure you’re coding something that you give a shit about. Otherwise, you’ll just lose interest and give up when you hit the first hurdle.
I am an avid runner. You’ll catch me running at least 5 days a week. One of my favourite quotes says “I don’t run to add days to my life, I run to add life to my days”. Without running, I’d have a very soft life. It’s far too easy to sit on my arse coding all day and not move. If there are any runners reading this blog here’s a link to my strava – if you’re reading this in the future, let’s hope I still bother to lace up my trainers!
Anyway, yes, I run, so I wanted to create an app that would help my running in some way.
Pace calculator
As a runner, you’re always wanting to run faster. Always asking questions like, ‘how fast do I need to run to get a sub 20 5k?’. ‘if I’m running at 5:45 pace for 5k how long will that take me?’. These questions are often followed by a laborious, and in my case, failed attempt at working it out.

So I want an app to do it for me.
The two scenarios I wanted to cover were the following:
- Calculate time based on distance and pace
- Calculate pace based on distance and time
Now, my first thoughts were that I could probably bung them all into the same UI and determine what the user wants to calculate based on the fields they’ve entered. But that feels a little boring and clunky to me.
I want to have two distinct sections to my app, one for each type of calculation. Luckily, BottomNavigationView exists and made this process a whole lot easier:

This allowed me to split my app into three distinct fragments Time, Pace, and Settings. Meaning the logic would be separated making the implementation easier.
Calculating time
Building Android applications is really simple and Android Studio does a brilliant job of splitting the XML files — where you place your UI components — by design and code. So really, you could create an app without looking at your XML files at all (like I did for the google tutorials).
The first fragment I implemented was Time, so given a distance and a pace how long would it take to complete the distance.
For this, I created a Calculator
class and a Pace
class. The function to calculate the time looks like the following:
class Calculator {
fun calculateTime(pace: Pace, distanceEntered: Double) : String {
val totalSeconds = distanceEntered * pace.seconds;
val totalMinutesInSeconds = (distanceEntered * pace.minutes) * 60;
val minutesAndSeconds = totalSeconds + totalMinutesInSeconds;
val minutesToDisplay = Math.floor(minutesAndSeconds / 60);
val secondsToDisplay = minutesAndSeconds % 60;
return "${minutesToDisplay.toInt()} mins ${secondsToDisplay.toInt()} seconds";
}
}
And the UI looks something like this:

All the UI is default Android studio, I’ve not changed any colours or customised anything (that wasn’t the goal of this exercise).
If the user entered an invalid number for Pace, for instance, 5:62 it would throw an error. It’s really easy to display a Toast with just one line of code:
Toast.makeText(this@MainActivity, "Distance must be a number!", Toast.LENGTH_SHORT).show()
A feature, that I don’t believe IOS has. So, when my app kicks off in the android app store I’ll have to rethink this when coding it up in Swift 😁.
Calculating Pace
I pretty much repeated the steps as above, copying and pasting the fragment and swapping out Pace with time.
I created a new Time
class:
package com.example.pace_calc
import java.lang.Math.floor
class Time(val hours: Int, val minutes: Int, val seconds: Int) {
init {
if (minutes >= 60 || seconds >= 60) {
throw Exception();
}
}
fun asSeconds() : Int {
var total = 0;
total += seconds;
total += minutes * 60;
total += hours * 60 * 60;
return total;
}
fun prettyPrintSeconds(seconds: Double): String {
val minutes = seconds / 60;
val secondsLeft = floor(seconds % 60).toInt();
var secondsOut = secondsLeft.toString();
if (secondsLeft < 10) {
secondsOut = "0${secondsOut}";
}
return "${floor(minutes).toInt()}:${secondsOut} per mile"
}
}
This code could really do with some unit testing, but as of writing this blog, I didn’t bother.

What’s next
You’ll notice that at the bottom right I have a settings cog, for a settings Fragment. That does nothing at the moment. I think the next step is to be able to specify whether you want the calculations to be in Kilometers or Miles. Me, being from England we mishmash the metric and imperial system. We drive and run in miles, we measure in centremetres, we drink pints and we lift in kilograms — it’s a mess.
For those of you that are interested in the source code, here’s a link to the GitHub repo.
Thank you for reading this blog, I hope you’ve enjoyed it! Please signup to my newsletter if you did.