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.

Want to learn Kotlin?

Check my free guide to create your first project in 15 minutes!

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:

class KotlinView : View {

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:

class KotlinView(context: Context?) : View(context) {

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:

class KotlinView : View {

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {


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:

class KotlinView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)


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.

Still not convinced about Kotlin for Android? Start using it as soon as possible! Thanks to the previous articles you can learn more about Kotlin, or I recommend that you sign up to my free training here.

%d bloggers like this: