When we saw the article about classes, you may remember that in general only one constructor is used. This is a problem for creating custom views.

The Android framework expects to have several constructors available based on where and how the view is being created (by code, using XML, if a theme is set…), so we can not ignore this case.

For this, Kotlin team provided the ability of having several constructors in the same class, just for cases like this.

Compatibility with Java is an essential requirement for Kotlin, so whenever you see yourself blocked, think there must be some way to achieve what you need.

Creating a Custom View in Kotlin

Even if you already have experience creating custom views and some Kotlin knowledge, it’s possible that the first time you create a custom view in Kotlin, you fin it a bit complicated.

Implementing several constructors is one of the most convoluted things in Kotlin, precisely because it’s a rare use case.

But don’t worry, once you’ve seen it once, the rest are very similar.

Note: this article is still valid to understand how to use multiple constructor in a Kotlin class, but as Kirill Rakhman mentions in the comments, there’s a better way. Read up to the end.

Create a class that extends View

To do this, create a class as we saw earlier. Make it extend from View for instance, but do not indicate any constructor:

You’ll see that this code won’t compile, because it requires calling the constructor of the parent class.

If you know that you are only inflating your view from Kotlin code, for example, you could use the unique constructor form we already saw:

But be careful, because if someone decides to add this view to an XML, it will fail.

Spoiler: Do you see that question mark right after the Context? In Kotlin, if we want a variable or parameter to be null, we have to explicitly specify it using a question mark. Then the compiler will force us to check it’s not null before doing anything with it. We’ll see it in next articles.

Implement the multiple constructors

The constructors use the constructor reserved word, and need to define in their declaration which constructor they call. It can be another constructor of the same class (using this) or the one of the parent class (using super).

This is how you define the constructors for an Android view:

An easier implementation

The alternative that Kirill mentions in the comments (thanks for that!) is much simpler and easier to read. It’s based assigning default values for constructor arguments, but needs a little tweak.

The thing is that when you create a constructor (or any function) with default values for some arguments, the generated bytecode only allows to use those default values in Kotlin. If you use that constructor in Java, you will be forced to specify a value for all arguments.

That is because Java doesn’t have this language feature. In Java, you would usually solve it by generating function overloads with all the variations you need.

You can get this code automatically generated by using the @JvmOverloads annotation in Kotlin.

The code would be like this:

As warned by Louis Cognault in comments, take into account that this solution will not be feasible for all types of views. Some of them (as TextView for instance) need to call super parent to initialize the style.

Conclusion

Once you see it, it’s not so complicated to generate your custom views thanks to the existence of multiple constructors, and even much easier using the @JvmOverloads annotation.

This can be useful for any class where you need more than one constructor. But in general, as you can assign default values to the parameters (and thus avoid overloads), it’s not so common to need more than one.

If you want to see all this and much more while writing a real App, I recommend you to take a look at my book Kotlin for Android Developers.

Author: Antonio Leiva

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.