Kotlin / anko

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

Styled blocks should use the (Context, AttributeSet, style: Int) constructor where possible #361

Open yanex opened 7 years ago

hatzel commented 6 years ago

I would love to see this fixed! I would be willing to take a look at this but I am not really sure where to get started. From what I gather the Factories for views are generated here: https://github.com/Kotlin/anko/blob/0f99e9fd5e4c8b44f5df645206005d3e2d7d958d/anko/library/generator/src/org/jetbrains/android/anko/render/viewRenderers.kt#L107-L112

So I guess you could generate code using the 3-argument constructor instead but I am really unsure of the details (like where to take the arguments for the constructor from). I will look into this some more but if anyone could provide a few details it would certainly help a lot.

filipproch commented 6 years ago

@hatzel Have you made any progress on this? I would also love to see this fixed.

I think the solution might be to have a factory lambda that takes style as second argument, and then ankoView function, that accepts factory with style argument.

I would also like to help on this, as I wouldn't have to hack around this in my code.

hatzel commented 6 years ago

No I have not made any progress, sorry. With some guidance I'd still be up for but I am mostly working on unrelated projects currently.

LouisCAD commented 6 years ago

I investigated in applying xml styles programmatically and saw that it's only possible with the 4 arguments View constructor available only on select views on API 21+, excluding Android support libraries/AndroidX views, so there's no viable option. The third parameter expects an attribute resource id, not a style resource id, so it only works for styles defined in the theme like buttonStyle.

Ok, Airbnb engineers found an over-engineered way to apply xml styles programmatically: https://github.com/airbnb/paris

ezralazuardy commented 4 years ago

@hatzel any progress on this bug?

hatzel commented 4 years ago

Nope, don't see this happening anytime soon for me. Not building apps at the moment and I'd probably be using flutter for new projects.

ezralazuardy commented 4 years ago

oh sad :( , btw I was able to style my button using themedButton(), but any background style is not applied to it..

LouisCAD commented 4 years ago

Just FYI, it's possible to hack the theme a little to be able to use xml styles. That's what I'm doing in Splitties Views DSL (and the Material and AppCompat extension modules).