Wednesday, October 16, 2013

Xtend & Android - Sneak Preview

In preparation of upcoming presentations for EclipseCon and W-JAX on that matter, I dived deeper into Android development and worked improving common programming idioms in that area. I'm contributing to Toby Kurien's Xtendroid and in this post I want to give you an idea of how Xtend can help developing Android applications.

Binding Android Layouts to Code

In Android user interfaces are mostly developed using Android's XML format for layouts. You can program them using Java but that's not what people do most of the time for various reasons. One of course being the verbosity of Java, but also the tools around the layout XML are quite helpful, especially when you have to deal with internationalization and need to support a wide range of devices.

Of course XML alone doesn't cut it which is why you have to bind the stuff declared in XML to Java code somehow in order to add listeners and other dynamic functionality. By default this is quite cumbersome as you have to deal with untyped code (cast) and a lot of integer constants. Some thirdparty frameworks try to reduce this problem by "injecting" the needed views reflectively, but that doesn't really help much. It's still not type safe and therefore error prone and you'll have to redeclare (i.e. duplicate) what you have declared in XML already.

With Xtend you can use an active annotation, which just points to the xml layout file. An active annoation is a macro annotation, which allows for participating in the compilation. This one will look into the referenced layout XML and declare local fields for any elements attributed with an '@id'. It also will derive an interface with all onclick methods declared in XML and make the current class implement it. As a result there is zero duplication and 100% type safety. The best is that the IDE knows about the changes and provides you with respective compilation problems, quick fixes, content assist and also the outline shows what has been derived from the XML layout file.

This is best shown in a small demo:

Xtend and Android - Binding XML Layout and Code from Xtext Team on Vimeo.

6 comments:

  1. Designing Android user interfaces in Xtend is much more convenient than in Java and quite an attractive alternative to XML coding. You can maintain a tree-like code structure, don't have to switch always between XML and program code, are much more flexible in your design, and can still use other Android features like internationalization an resource handling. All you need is a very small set of very simple extension methods. I wrote an article in JavaSPEKTRUM about this approach (to be published in November this year).

    ReplyDelete
    Replies
    1. Oh, cool. Would be very nice to read your article. Could you send it to me via mail?

      Delete
  2. A great contribution to Android development, because it's no fun to write code for XML layout elements.

    ReplyDelete
  3. I have attended your talk at the W-JAX and was quite impressed by the possibilities of Xtendroid. While the impressions are still fresh I spared a little time to try it by my own. It is really as simple as it seemed to be while watching your demos. Great job.

    You had this part with the annotation for creating an entity. Are you thinking about to share this as an example on GitHub anytime soon? If not is it possible that you send me the code as mail? I plan to do a little internal demo for my colleagues and I think thy will like that part too.

    ReplyDelete
  4. Sorry for the late reply. The entity stuff was a total prototype. I'm going to make it a bit more useful and will upload it somewhere and tell you. But it may take some time...

    ReplyDelete
    Replies
    1. Don't bother. I can imagine that your workload is heavy these days.
      I'd take a look at the Github repository from time to time. I think it would be a rather cool additional example.

      Delete