public MyEditText(Context context) { this(context, null); } public MyEditText(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs); //My custom initialization code }
Here, the constructor are calling each other by adding the missing argument by a default one. However, this didn't work as I was first expecting it. When running the program, the widget was simply not displayed.
Looking closer at the widget constructor, EditText for instance, I realized that the widget is actually defining the default style.
public EditText(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.editTextStyle); }
In order to respect the default styling, I modified my construction code for customized widget to the following:
public MyEditText(Context context) { super(context); initialize(); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); initialize(); } public MyEditText(Context mContext, AttributeSet mAttrs, int mDefStyle) { super(mContext, mAttrs, mDefStyle); initialize(); } private void initialize() { //My custom initialization code }
This way, I minimize the amount duplicated code and fully respect the framework styling.
No comments:
Post a Comment