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
With our new formatter for example you'll see your string values formatted like this:
On the other hand, a layout file will be formatted like this:
And here's a manifest file:
Another key feature of the formatter is that it can sort the attributes, either alphabetically or in the "preferred" Android order:
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.