Search code examples
androidkotlinextend

Android Kotlin - How to extend ConstraintLayout?


I'd like my ConstaintLayout to carry extra additional properties, but I have trouble extending it. More precisely I have trouble putting correct constructor into

class myCL(): ConstraintLayout(???) {
}

Solution

  • The constructor you really need is the one with all arguments:

    class myCL(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
        ConstraintLayout(context, attrs, defStyleAttr) {
    }
    

    if you want to implement all three constructors without much hassle you can use @JvmOverloads and use sensible defaults.

    class myCL @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ConstraintLayout(context, attrs, defStyleAttr) {
    }
    

    see https://developer.android.com/reference/android/support/constraint/ConstraintLayout