Imagine that you need to do some long time operation and update UI with its result. You probably know that you should not call things that might take some time on UI thread. If you do so, an application might become unresponsive and that would lead to poor user experience or system stepping in and offering to kill an application with infamous Application is not responding dialog.
So how can you avoid this? There are many ways. Most commonly used are threads, AsyncTasks and now also Kotlin coroutines. In this article, I will make a quick introduction and provide reference links for more details.
Continue reading
Category Archives: Kotlin
Default Interface Methods in Java 8 and Kotlin
Before Java 8, interfaces couldn’t implement any code. With Java 8, interfaces can include default methods implementations and also static methods.
Default Methods
Default method is method implemented in interface marked by the modifier default. These methods can be called from all classes implementing the interface. What is it good for? Let’s imagine a game with a lot of heroes that have can do some actions. E.g. going left and right. After while add a new action, for example jumping. Now we have to implement this action in all methods. And maybe this action is same for all heroes or we don’t need this action to be used by all heroes. The implementation of second option could look like this:
Continue reading
Kotlin Higher-Order Functions
A higher order function is a function that takes a function as a parameter or it returns a function. It allows us to write shorter and more beautiful code. Let‘s look at some examples.
fun <T> tryAndCatch(block: () -> T) = try { block() } catch (e: Exception) { println(e.message) }
This function will run code passed inside block variable inside try catch block and return result and also print exception in case it is thrown. What does it all mean?
Continue reading
Kotlin Data Classes
The main purpose of many classes is to hold data. But to effectively manipulative with data, we need to override quite a lot data. Class holding 3 String values would typically look something like this in Java:
Continue reading
How to check object equality in Kotlin?
Unlike Java, Kotlin is purely object-oriented language. There are no primitives, everything is an object. With this said, you might be surprised by results.
Let’s compare two objects with equality operator:
object1 == object2
You expected to get false, right? But answer is true. Why? This code is translated to
a?.equals(b) ?: (b === null)
Kotlin Constructors
Kotlin helps you make your code shorter and more readable in many areas. One of them is constructors. In Java you have to tediously assign all variables passed to the constructor in similar style
this.something = something
If more constructors are required, you have to overload them and repeat yourself over and over. Like in the following example.
Continue reading
Static Methods and variables in Kotlin
I presume most Java programmers write some helper static methods and most probably even have some helper class including just these. So sooner or later you will need to write static methods like this in Kotlin.
public static float getPxFromDp(Context context, float dp) { Resources r = context.getResources(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); }
But you will find out you there is not anything like a static method in Kotlin. Fortunately, there are ways around this.
Continue reading
Kotlin Edu
There is a lot of ways how to learn Kotlin. Language reference, tutorials, video tutorials, online courses and much more. And now also Kotlin Edu.
Kotlin Educational Tools is Android Studio Plugin that helps you learn Kotlin by giving you small tasks. Read task description, write code and check. And if you cannot find the solution, you can click on Fill answer placeholders to get a right answer. Task description usually includes a link to Kotlin reference page, so you get right at the page with all you need to study to finish a task. It first looks like this.
Continue reading