New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Don't chain constructors with this(). #30
Comments
Actually, calling "this" for the CTOR is very common, and it's also how Google made its own custom views. Here's the example of AppCompatTextView:
This also (optionally) allows "final" fields, which depend on the CTOR. |
Sure, but bypassing default styling can still be very problematic (as it was for me in this case). If you'll permit an Appeal to Authority here's the relevant part from the Android SDK: http://developer.android.com/guide/topics/ui/custom-components.html#modifying
While I'm here, very many thanks for creating this component... it saved me a ton of work! |
Both ways are ok and work fine. Using "this" has its own benefits. |
Here're the CTORs of TextView, btw:
|
Why do you think the SDK even mentions it? If you subclass TextView, as your lib does, how do you propose to apply com.android.internal.R.attr.textViewStyle as per TextView's constructors? Obviously neither approach is intrinsically wrong - there could be perfectly valid reasons for avoiding particular versions of super() - but this isn't one of them. As you'll find out if you ever extend your lib to provide an autofitting EditText. |
I've updated the code. Now "this" should work fine. |
Looks good to me, I didn't realise attr.textViewStyle was accessible... the one in TextView/EditText source code refers to an internal package but it seems probable they're identical. |
well if it's accessible for compat, it should be accessible for me too. :) |
The problems caused by this() are most evident when changing the base class from TextView to it's close relative EditText. The EditText behaves rather oddly because it's never gets set up with some important default internal styles (see Android source for EditText(Context, AttributeSet)).
Your current code has the two simpler constructors calling the 3-argument constructor via 'this(context, attrs, 0)'. The derived constructors should be calling super(), not this() and also calling a common init() method which contains the autofit init code.
The text was updated successfully, but these errors were encountered: