October 2017
Intermediate to advanced
294 pages
6h 49m
English
On our custom view, we've created a single constructor with two parameters, a Context and an AttributeSet. Now, if we're creating our UI programmatically, or if by any other reason we need to instantiate our custom view by code, we need to create an additional constructor.
As we want to keep using our custom view in our XML layouts, we have to keep both constructors. To code avoid duplication, we will create some helper methods to initialize it and use them from both constructors:
public OwnCustomView(Context context) {
super(context);
init(DEFAULT_FILL_COLOR);
}
public OwnCustomView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
int fillColor;
TypedArray ta = context.getTheme().obtainStyledAttributes(attributeSet, ...Read now
Unlock full access