I have my custom view extended from View. There are 3 view constructors:
View(Context context, AttributeSet attrs, int defStyle)View(Context context, AttributeSet attrs)View(Context context)
From my activity I call std.setContentView(R.layout.main). The second constructor is getting called in my view. Why the second one? How to know in advance which one will be called and why?
From the Android developer site under documentation for View:
public View (Context context)So this constructor is what you can use to create a View in Java. It will not be called when you inflate from XML.
public View (Context context, AttributeSet attrs)So this constructor will be called when you inflate a View from XML when you don’t specify a style.
public View (Context context, AttributeSet attrs, int defStyle)You should implement all of these constructors, but you can put all of the work in the third one by calling
this(context, null)andthis(context, attrs, 0)for the first two, respectively.