Reading my previous blog post some people asking why I’m so strongly against
XMLs if they are really good? I think I shall clarify that I’m not against XMLs
per se, I’m against the way many developers organize their apps. Let’s look at
a simple example - a button and a textview showing how many times the button
was pressed.

So far they are very similar, but we can already see that View and Controller
are clearly separated in the second example. Is it helpful?

Let’s now have two buttons - increment and decrement, and let’s the range of
number will be between 0 and 10. Increment button should be disabled when the
number reaches 10, decrement button should be disabled when the number reaches
0.

See what happened? I only changed the requirement about View (another button,
enabled/disabled state should be controlled) - but we had to rewrite most of
the existing Activity code. That’s because Activity is not a pure Controller.
It’s a mix of View and Controller that are so tightly coupled that you can’t
use them separately.

With Anvil where view and controller are decoupled this change won’t be even
noticeable:

I only added one line to the Controller part (because we really need to handle
our model differently, so the controller had to change), and I modified the
view, because it was requested to control the enabled state of the buttons.

If later the requirement becomes to hide buttons instead of enabling them - I
will have to modify the view only, not the controller. But if the change will
be to reset number to zero instead of decreasing it - I will modify the
controller only, not the view.

Now, let’s also add a Slider to control the same numeric value. Of course when
the value is changed using the buttons - the slider should be adjusted, too.

With Android+Knork we now have too many places when we need to modify the state
of our views, so we move it to a separate method called “render” to avoid
copy-paste:

Now, imagine if render() is called automatically in your UI event listeners?
You’ve just re-invented Anvil! But one important note on performance - Anvil
renders only the parts of the layout that have actually been changed, not all
of your UI widgets.

Summing up. Declarative event binding is much more readable than setting
listeners manually. It’s as good as using Knork or ButterKnife or RoboGuice or
AndroidAnnotations. Except for with Anvil it gives you automatic smart
rendering of your UI after your event listener has been finished.

Rendering all your layouts at once is nice way to ensure that your model and UI
are always in sync, no matter how complex your UI logic is. But it can be too
complicated if you have lots of UI widgets and you want to change the text of
just one of them. So Anvil does this automatically for you.

Finally, decoupling views and controllers is a good way to keep your app code
clean and maintainable.

P.S. For those who missed the birth of Anvil last week -
here’s the repository