Refactoring Your Java Android App to Kotlin

With the release of Android back in 2008, the go-to language for app development has always been Java – however, that’s beginning to change. At Google IO 2017, it was announced that Android would be receiving “first class support” for Kotlin. Since then, the language has been slowly ramping up to becoming the preferred language for development on the Android platform. In the following article I’m going to give you some ways you can refactor/rewrite your existing Java app code to Kotlin, while also pointing out the advantages Kotlin gives us. But what about Java? I still feel like Java is worth knowing, since a lot of the Android APIs, as well as their documentation, are written in it. If you’re just starting Android development, I would reccommend developing at least an intermediate level of Java knowledge before moving on to Kotlin. If you’d like an in-depth guide to getting started with Android development, you can read an article I wrote detailing how to become an Android developer in under a year. I should also mention that Kotlin is interoperable with Java. For this reason, you aren’t forced to choose one or the other. You’re able to call Java code with Kotlin and vise versa. So, don’t feel like you’re wasting time just because you’re learning Java. Refactoring Java to Kotlin I’m going to present some Java classes from my own personal projects, with the Kotlin counterpart following. The advantages Kotlin presents should be evident from simply looking at the code, but I’ll be sure to go through each. SharedPreferences

Here’s an implementation of an interface used for managing a few SharedPreferences  values. The class is quite straight-forward, as far as preference managers go. Let’s take a look at the same class in Kotlin.

Immediately, you can already see …

How to Become an Android Developer in Less Than a Year

The title got your attention didn’t it? Now don’t worry, this isn’t clickbait. In the following article I am going to tell you everything you need to know about what it takes to become a professional Android developer. However, don’t think you will just be able to close your eyes and somehow one year from now you will be in this position – it is going to take a lot of work. Achieving this quickly is going to require dedication and a strict routine. In my opinion, you will need to set aside 2 – 4 hours a day, at least 5 days a week, to studying. For 99.9% of people – this is completely doable. Also, if you have intermediate programming knowledge, this can be done in 6 months or less.   When should I study? Whenever you can set aside a solid window of time. For most people in school or those with a full-time job, the best time would be in the evening. In the case of students, just dedicate at least 2 hours a day on top of your normal studies. Do not be tempted to go out and get wasted on the weekend, or even worse, during the week. You have little to gain and a lot to lose by engaging in this behavior – I’ve been there and it’s not worth it. For those with a fulltime job – I know this can be extremely difficult. By the time you get home from work you are probably already exhausted. This feeling can be exacerbated if you are working a job you don’t like. On top of this, you need to make dinner and wash the dishes (if you are not blessed by the holy invention known as the dishwasher). I feel your pain. However, there are a …

Time & Space Complexity in Functions – Big O Notation

Knowing how fast a function will run or how much memory it will require when running are important factors to consider when writing algorithms. This may not be something to consider in small, simple programs, but just imagine a large project riddled with unoptimized and slow performing functions. It can have a drastic impact on overall system performance. Luckily, there is big O notation, which allows for quick deduction as to how fast a function will perform or how much memory it will require. Introducing, Big O Notation Big O notation (also known as asymptotic notation) is simply a mathematical notation we can use to measure the performance of a function. There is no math involved in this approach, rather you only need to look at the code in order to classify it. The main way we go about achieving this is by determining how fast the runtime will grow in relation to the size of the given input. For example, some functions will have a relatively quick runtime with a small input, but as input grows larger, runtime can increase exponentially. This will be clear after looking at a few examples. Time Complexity Constant time, O(1) Let’s take a look at a very simple function.

No matter what the size of the input, this function will always take the same amount of time. We deduce this by the fact that there is always only going to be one “step”. Our String can be one or one thousand characters – it will still take the same amount of time to print both inputs. In this case we can say that this function runs in O(1) time, or rather, constant time. Linear time, O(n)

This function will run in O(n) time or what is known as linear time. This is due to the fact that …

What Are Higher Order Functions and Receiver Types in Kotlin?

If you have been programming Java, especially within Android, for a long time, you would know that higher order functions are not really a thing. They are, in such a way that you can pass around a functional interface, however those are not true functions (a function literal to be specific). Fear not, because Kotlin is here and allows you to do just that. A basic higher order function Kotlin allows us to define a function which takes a function (a literal) as a parameter. The syntax for this looks like the following:

We’ve written an empty function which takes a function as a parameter (simply named function  in our case) and does nothing. You should notice that we wrap our argument types in brackets, if we wanted multiple parameters, we’d use a comma to separate them. Right now, this function isn’t doing anything. Let’s change that.

From our main  method, we are now passing a lambda function to functionTest() . We can see that it takes value s  and returns"!") . All this lambda is doing, is just taking a String and returning the same String appended by a “!”. A little more complex… Higher order functions can grow to be more complex. Let’s see an example where we take a function with multiple parameters on top of having multiple statements in the return clause.

If you’re new to Kotlin, this can look a little confusing. Especially the last line of our main  method. For starters, the last line of our return clause is what is returned. We do not have to include the return  keyword for this – it can be omitted. So, we’re allowed to have an unlimited number of statements, as long as the last line is what is to be returned (a String in this case). Also, as previously stated, we separate …

Playing Sounds with SoundPool in Android

Like MediaPlayer, SoundPool can also be used to play audio files. However, unlike MediaPlayer , SoundPool  is more suited for quick sound effects as opposed to longer audio files which require streaming. Before we even begin, we’ll need some sounds to work with (preferably ones with a creative commons license, or similar). You can use google for this or if you’re lazy,   Storing our sounds Download any number of sounds you like. In your project, create a new folder under app called assets (do not store your sound files in the res folder). You can achieve this by right clicking on app, New -> Folder -> Assets folder. Your project structure should look like the following afterwards, Once you’ve created your assets folder, create another folder within assets. You can name this folder anything you want – we’ll use it for storing our sound files.   Accessing our sound assets To take steps to make our code clean, we will be making a class dedicated to loading and playing our sounds outside of our Activities and Fragments. We’ll start with building that class now. Create a class called SoundPlayer. We’ll need a few methods which will be used to hide away the logic of accessing and loading our sound files from outside classes. Here’s my implementation of SoundPlayer . You can change/add some code to suit your needs but this is the basic gist.

The core components of this class consist of: List  for storing our Sound  objects AssetManager  for accessing our app’s asset files. SoundPool  for playing our sound files. You will also notice two different constants, SOUND_FOLDER  and MAX_SOUNDS . They specify the name of the folder we’re storing our sounds and the maximum amount of sounds we allow our SoundPool  to play at once, respectively. fetchSounds()  is executed as soon as our class is instantiated. It first tries …