3 ms·
Kotlin is a very nice language especially from the Android developer perspective. It's so concise that you may not even use an IDE having only a text editor ins
by zserge 11y ago
Kotlin is a very nice language especially from the Android developer perspective. It's so concise that you may not even use an IDE having only a text editor instead.
In addition to Anko, there is also a similar (but much smaller) library that I maintain - https://github.com/zserge/anvil https://github.com/zserge/anvil
It is also a DSL for defining view layouts in code, much like Annko. The major difference is that Anvil supports data binding and has reactive nature (think of it as of react.js for android).
- kcorbitt 11y agoHuge "+1" for Anvil (and thanks so much for developing it). I've written a fairly large app in production using Anvil+Kotlin, and developing with it feels more like React than the traditional Android imperative slog. At the same, the library gives you the flexibility to easily drop down and access any Android APIs that don't fit into its paradigm. I've been working more with React Native recently because I'm working on a cross-platform project, but for Android-only development the combination of Kotlin and Anvil gives you pretty much the same level of declarativeness and conciseness while still enforcing a decent type system and not being so far removed from the platform.
- zserge 11y agoJust made a big push to Anvil repository - it can now inflate layout from XMLs! This means you may use your favourite IDE to design layouts, then use Anvil to bind data and event listeners to make your UI reactive: public override fun view() { // A designer gave us an XML with some fancy layout: // a viewgroup with a button and a progress bar in it xml(R.layout.my_layout) { backgroudnColor(Settings.bgColor) // will modify root layout view color withId(R.id.my_button) { // button state may depend on some variable enabled(isMyButtonEnabled) // button click listener can be attached onClick { v -> ... } } withId(R.id.my_progress_bar) { visible(isMyProgressBarShown) progress(someProgressValue) } } } I know many people are opposed to defining layouts in code, so this can be a good compromise for them. Also, it's very helpful if you have a large project made with XMLs and want to refactor it.
- habosa 11y agoThat's fantastic! Can you upgrade the README with a little detail on this?
- zserge 11y agoDone. I will try to cover more within the next few days, hopefully I will add an example to `anvil-examples` repository and make a blog post tell more about recent changes in Anvil.
- PeCaN 11y agoWoah, that's very useful. Might even be your killer feature, actually. Totally checking this out.
- on_and_off 11y agoI really like that projects such as Anko & Anvil exist but I am not a fan of declaring layouts in the middle of the Activity's code. I am have nothing against getting rid of XML : it is not that bad in practice since the IDE writes the boring parts but still ugly to read. Separating the presentation from the code is an important feature though, especially for very large applications.
- zserge 11y agoThat's why Anvil has the lightweight concept of "renderable" - it's just a lambda with view hierarchy declaration, which can be put anywhere you find appropriate in your architecture - in activities, fragments, in self-contained components, in special dedicated "view" layer classes, you can move common parts into separate styles module etc etc. Since you have the full power of your programming language you are free to refactor it any way you like. Anvil has no restrictions on that.
- on_and_off 11y agothanks for replying. tbh, I have not tested Anvil yet, I should, if only by curiosity. Suggestion : I don't see anything regarding density buckets, composability with merge tags or other strengths of the existing system on the repo readme. It might be a good idea to treat these as well.