CodeIdeal
09/02/2020, 4:12 AMpublic class CustomView extends View {
public CustomView(Context context) {
this(context,null);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//some custom code
}
}
I try init{}
block, but it run before constructor.
class CustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
init {
// this place is run before super constructor
}
}
Kavita
09/02/2020, 4:26 AMclass CustomView : View {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
//some custom code
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs, 0) {
}
constructor(context: Context?) : super(context) {
}
}
@CodeIdeal This is the other way to implement the same code.
Hope this helps.Ian Lake
09/02/2020, 4:28 AMCodeIdeal
09/02/2020, 5:02 AMKavita
09/02/2020, 8:17 AM