Recent Changes‎ > ‎

XML Formatter

posted Aug 10, 2011, 4:35 PM by Tor Norbye
As mentioned here, we've revamped the XML editing support in Eclipse recently. One key feature we've improved is XML formatting. 

The Android Eclipse plugins now provides its own custom XML formatter. This formatter replaces the default Eclipse XML formatter, but there is a setting you can uncheck to revert to the default Eclipse XML formatter.

The new formatter basically formats the XML the "standard" Android way out of the box, following the formatting conventions used in the various Android documentation, tutorial and platform source code. In particular, this means that it is going to format the XML differently depending on whether the XML is in a resource value file such as strings.xml, or a layout file, or a manifest file,and so on. This is one of the main reasons we provide our own formatter, because general text editors and IDEs tend to apply a single set of formatting rules to all files. 

With our new formatter for example you'll see your string values formatted like this: 
As you can see, each element along with their attributes and values are defined on a single line, and there is no spacing between the lines.

On the other hand, a layout file will be formatted like this:
Here you can see that there is a blank line between each view element, and the attributes are spaced out on separate lines (except the original xmlns: attribute which is placed on the same line as the root element).

And here's a manifest file:
Here the elements of the same type are grouped together, so you get a single block of <uses-permission> elements.

Another key feature of the formatter is that it can sort the attributes, either alphabetically or in the "preferred" Android order: 
  • id
  • style
  • layout width and layout height
  • other layout attributes, sorted alphabetically
  • remaining attributes, sorted alphabetically
This is on by default, so if you open an XML file and hit Ctrl-Shift-F to format the document, it will not only reformat your XML but it will reorder all the attributes to follow the above order as well.

Finally, we now support "Format on Save", which is just like the equivalent action in Java files: It automatically applies a format on the whole document before saving the file.

By default, the formatter will use the Android-defaults for indentation: 4 space characters. However, there is a setting for turning this off and making the indentation use the Eclipse XML indentation settings instead (which defaults to one tab character). Here are the current options related to the XML formatter:

Also note that files created with the New XML FIle and New Android Project wizards are now run through the formatter, so they should pick up any custom formatting configuration settings. Also, the new formatter tries to be smart about "partial formatting", where you've selected a range of text and you want to only format that portion; it tries to preserve the indentation level surrounding the node, and it can also format for example only the opening tag, which is useful when you set an attribute on a node that has children, and you expect only the opening tag to be reformatted to accommodate the attribute change and the child content should be left alone. These changes are important for the "Automatically format the XML edited by the visual layout editor" setting.

We would love feedback on the new formatting features in case there are important configuration parameters for us to add, such as other valid sorting orders, other formatting tweaks, and perhaps even more importantly, bug reports for scenarios where the formatter does not handle a particular XML file properly.