Do I need all three constructors for an Android custom view?
Long story short, No, but if you do override any constructor, then ensure to call super(...)
with the exact same number of arguments (like, see Jin's answer for example why).
If you will add your custom View
from xml
also like :
<com.mypack.MyView
...
/>
you will need the constructor public MyView(Context context, AttributeSet attrs)
, otherwise you will get an Exception
when Android tries to inflate your View
.
If you add your View
from xml
and also specify the android:style
attribute like :
<com.mypack.MyView
style="@styles/MyCustomStyle"
...
/>
the 2nd constructor will also be called and default the style to MyCustomStyle
before applying explicit XML attributes.
The third constructor is usually used when you want all of the Views in your application to have the same style.
If you override all three constructors, please DO NOT CASCADE this(...)
CALLS. You should instead be doing this:
public MyView(Context context) {
super(context);
init(context, null, 0);
}
public MyView(Context context, AttributeSet attrs) {
super(context,attrs);
init(context, attrs, 0);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
// do additional work
}
The reason is that the parent class might include default attributes in its own constructors that you might be accidentally overriding. For example, this is the constructor for TextView
:
public TextView(Context context) {
this(context, null);
}
public TextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
If you did not call super(context)
, you would not have properly set R.attr.textViewStyle
as the style attr.