Ductulator: Android App In Kotlin

Slava Krel
4 min readSep 23, 2020
Photo: Louis Tsai

Intro

Let’s create an Android app that allows you to do duct calculations. Speaking from experience, this app became a handy tool I utilize daily working with mechanical contractors. Last time I finally figure out all the duct air velocity, sizes, and friction loss dependency and posted “Taking the mystery out of equivalent duct calculations”. Of course, there are many online duct calculators, but some of them limited and only available if you have internet. On a job site you need something in your pocket, native — you can use either you have internet or not. This app is a phone version of the paper Ductulator that some of you very familiar with, have seen a lot, and which you can’t find when you need it.

Paper Air Duct Calculator

Why Kotlin? Even though Kotlin becomes more popular in the android development, there are not a lot of tutorials about “how to do math in Kotlin”. Maybe it’s happening because of Java similarities, but you cannot just copy the Java code into a Kotlin application and expect it to work.

With that said, I hope this post will be relevant for those of you who are learning Kotlin. The purpose of this post is to show you a one-screen app development. We are going to code only one layout file (activity_main.xml) and one class (MainActivity.kt). I will touch on a few important things that I haven’t been able to find online. It’s not a step-by-step tutorial. But if you are at the stage when you are trying to work with math equations in Kotlin, I’m assuming you have a pretty good understanding of the language and the work environment.

Here you will learn

  • Addition, Subtraction, Multiplication, Division, Exponent in Kotlin
  • Round function in Kotlin

Stack

Android Studio 3.5.3, Kotlin

Activity_main.xml

As I mentioned earlier this is a one-screen app, so, in the first step, let’s find out what the app is going to look like. For the calculations we are going to use formulas from here.

Let’s say we want to find Equivalent Round Duct (de), Air Velocity (v), and Friction Loss (dh) based on given rectangular Duct Sizes (h and w) and Air Volume Flow (cfm). It boils down to six parameters we have to deal with. In order to make it simple, I’m going to use Imperial system for all the calculations and galvanized steel as a duct material.

The user needs to input duct size, both height and width, and the air volume flow. Once the user presses the Enter button, we are going to have Kotlin make the calculations to find Velocity, Friction Loss, and Equivalent round duct size. Also, and more importantly, we’re going to code Equivalent rectangular duct sizes.

I’m placing the input parameters on the top of the screen using EditText field and using a TableView to line things up. The user must put numbers here, so make it easier for him:

Then place all the EditText field for all the calculated parameters.

Place the Enter button at the very bottom of the screen.

Main Activity

Since this is a simple app where all the calculations have to be done at the same time, I will execute my onClickListener at the beginning.

Look at the code above. To get data (numbers) from EditText field into my calculations I need to convert them into Double type variables. The math operations such as Addition, Subtraction, Multiplication, and Division in Kotlin are pretty easy to utilize. However, Kotlin doesn’t have an exponent operator. But Java has Math.pow, which I’m about to use with Kotlin. Kotlin also has extension functions for Float and Double that you can use instead. Now look at the line #13 – This is what the equivalent round duct size calculation looks like.

If you want to round a double value to specific decimal places and print it, you can use java.math.BigDecimal. Line #14 shows the BigDecimal rounding features several RoundingModes, including those rounding up (away from zero) or towards positive infinity.

For the equivalent rectangular duct size calculation I’m going to use the most useful variety — duct height two and four inches lower.

Conclusions

As a BIM Engineer I understand that it makes sense to make a Revit/Navisworks adding, because those are the most useful software. I also think it would be beneficial if you or your field team have this offline duct calculator in a pocket. I’m thinking it would be nice if I can hook it up to the central model with Autodesk Forge so I can markup the ducts I changed or ducts that need to be changed. Anyways, let me know what you think.

See the full code on my GitHub.

--

--