I was browsing the Android commit tree, as I like to do at 3:20am sometimes, and I just saw a new commit by Tor Norbye with the following description that made my heart skip a beat: "Add autoformatting of XML." This little update may not mean much to the regular folks, but to Android developers, like myself, this has been a long requested feature.

About a year ago, I wrote this article: Auto Formatting Android XML Files With Eclipse, which described how easy it is to achieve uniform, formatted XML files in Eclipse while doing Android development. Since XML files comprise a very large portion of an average Android project (the whole app layout is XML based), keeping XML files tidy becomes a very important, but mundane task.

You see, unlike the Java formatter, there is no option in Eclipse to auto-format XML upon save. Thus, it is left to the developer to not keep firing up the formatter, which quickly gets tiresome.

Here is the full description of the commit, for the interested parties:

This changeset improves the formatting of XML edited by the layout
editor in two ways:

(1) It improves the way the layout editor handles insertion and
deletion into the XML document; it looks up the indentation of the
sibling and parent elements and attempts to correctly indent new
elements based on the existing surrounding formatting, and it also
attempts to clean things up correctly on element deletion.

(2) It adds a new user option for turning on automatic XML
formatting. When this is on, it will invoke the Eclipse XML formatter
on portions of the XML after each edit. This will ensure that the
document adheres to the user's preferred formatting settings (maximum
line width, tabs versus spaces, line breaks before attributes, etc.

Change-Id: I74f9a4240a8c5ca4295c01f3b55751ef10b1c1b0

This change, likely to be released together with the next SDK (Gingerbread), is coming to an Eclipse near you. Hooray!

/end of geek excitement

Source: AOSP git

Artem Russakovskii
Artem is a die-hard Android fan, passionate tech blogger, obsessive-compulsive editor, bug hunting programmer, and the founder of Android Police.
Most of the time, you will find Artem either hacking away at code or thinking of the next 15 blog posts.

  • Double

    I rather have auto-complete for the xml. I always forget all of those names and atributes.

    • http://www.AndroidPolice.com Artem Russakovskii

      There is autocomplete for xml - it completes resources, tag names, parameters - everything.

      • Double

        Hmm, tried it, and I suppose it does. It takes a while to load, suppose i type to fast. :p

        • Denbo

          You are right sometimes autocomplete works fast and other times it seems to do nothing.

  • http://www.anivision.org Xcom923

    OMG!! I'm still new with android development but the one thing that annoys me is that while eclipse can format the java code it never does the XML I have it set to auto reformat when I save so this would be a great help

  • http://tornorbye.blogspot.com/ Tor Norbye

    Hi Artem,
    Just to clarify: That option (Auto-Format XML) was added to the options for the new visual Layout Editor, so it only applies to the edits the layout editor itself is making, on your behalf, when you are graphically editing a layout file.

    It sounds like you would like a more general save-option which would run after any XML file is saved, regardless of the editing source? Adding that shouldn't be hard; we already trigger some actions on these file saves (such as the file-include support we just committed).

    • http://www.AndroidPolice.com Artem Russakovskii

      Hi Tor, thanks for responding so quickly after my tweet. I see the difference now - it'd be brilliant if you could add the generic auto-format on XML save (though, frankly, I think it should be part of Eclipse core).

      Also, while I have your attention, I'm absolutely stumped by the fact that in some fields while editing properties, you can press Delete, and in some it does nothing, and only Backspace works. Likewise, ctrl-V doesn't work, and neither does ctrl-Z. Such basic things that we all take for granted are missing in the ADT, and I would kill to have them present.

      Also, in the new Layout editor, there is a bug with the scrollbar on the right of the View list (where it shows all views), 2 bugs even. First, if you scroll it, it scrolls by maybe 1 millimeter at a time. 2nd, if you collapse and uncollapse the view group, the scrollbar disappears.


      • http://tornorbye.blogspot.com/ Tor Norbye

        Hi Artem,
        would you mind filing these as issues over on http://b.android.com ? Especially for the deletion key thing, more details on exactly which fields you're referring to. I know I've fixed something in that area (e.g. https://review.source.android.com//#change,17764 ) but I'm sure there are many different places that it might occur as well.

        (File with Component=Tools)

        • http://www.AndroidPolice.com Artem Russakovskii

          Filed 3 bugs, thanks.