I was trying to implement CustomView
in Kotlin
, which is to be used both for programatically and for statically. Thus, I need to override both versions of constructors.
For programatically I use version,
class CustomView @JvmOverloads constructor(
context: Context,
) : View(context)
For statically I use version,
class CustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : View(context, attrs)
How can I change it for overriding multiple versions under same class which then I can instantiate from static views as well as programatically?
There are some post on constructors i.e. Kotlin secondary constructor which does not help for overriding multiple versions of constructor.
This should work both programatically and statically :-
class CustomView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr)
Programatically just call :-
CustomView(context) // passing other params to constructor is not mandatory..