Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.88k stars 1.29k forks source link

Please add the (Context, AttributeSet) constructor to Anko _classes #303

Open mvysny opened 7 years ago

mvysny commented 7 years ago

For better interoperability with Android it would be great if Anko could provide the (Context, AttributeSet) constructor. The use-case is as follows: I would like to gradually convert parts of my UI to Anko, and inflating those parts as Views from the original UI XML. However, that's not possible because of missing constructor (Context, AttributeSet). If I don't override that constructor, Android will complain:

Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                                                                      at java.lang.Class.getConstructor(Class.java:528)
                                                                      at java.lang.Class.getConstructor(Class.java:492)
                                                                      at android.view.LayoutInflater.createView(LayoutInflater.java:592)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 

On the other hand, if I introduce just a dummy constructor constructor(ctx: Context, attrs: AttributeSet?) : super(ctx) then Android won't process even the android:id parameter, and this will result in NPE later on when findViewById won't find anything.

kevinmost commented 7 years ago

++ on this, this is really important for interop in my opinion. It would be great if I could make my custom views extend _LinearLayout, for example, so that I can access lparams as I create them, but extending _LinearLayout would make my view broken for XML inflation.

Miha-x64 commented 7 years ago

I guess, that when multiple receiver support in Kotlin will be implemented, these classes will go away.