04
Nov
image
Last Updated: April 16th, 2010

This post and all its comments were migrated from Artem's personal blog beerpla.net when Android Police launched. If you would like to visit the original post there, please click here.

In this article I will describe the problem I've had with Eclipse's handling of XML file formatting as well as the best way to fix it.

I use Eclipse to do my Android development for a few reasons:

  • it's the only IDE fully supported by the Android dev team
  • it has a visual Layout/Resources builder that transforms XML files into corresponding visual representations
  • it's free and open source
  • I've been using Eclipse for many years and am very familiar with it

In order to use the visual features in Eclipse when developing for Android, you need to install the ADT plugin provided by Google

The Problem

However, one thing about Eclipse Android development has bothered me for a while - and that is XML formatting by the visual tools. See, if you open an XML file in Eclipse and use the Layout/Resources tab (which functions as either a visual UI builder or a visual frontend to resource management), Eclipse creates the XML representation of what you're building and dumps it into the file you're editing. The problem is, this XML is not formatted in any way - Eclipse just writes it in a single line, which looks absolutely horrible and is essentially unusable.

For example, I had this beautifully formatted XML file with a sample Android menu. After using the Layout tab to add another item, I ended up with an single ugly unindented line for it.

image

The Solution

Here's how to solve this in an almost fully automated fashion, using formatting rules that I think result in the most readable file. There is no need for external tools - Eclipse provides everything needed.

  • open up Window->Preferences->XML->XML Files->Editor
    image
  • check the "Split multiple attributes each on a new line" and adjust other options according to your liking (such as Indent using spaces and Indentation size)
  • press OK to save the options
  • all you have to do now is press Ctrl-Shift-F or select Source->Format

Let's see what this does to the XML I showed above:

image

It's a beauty, isn't it? Eclipse's Auto Format is one of its best features (yes, you can apply it to your Android Java code too as well as pretty much any other document format).

Bonus - XML Cleanup

As an added bonus, Eclipse also offers a Cleanup XML option, which can do the following:

  • compress empty element tags
  • insert required attributes
  • insert missing tags
  • quote attribute values
  • format source (as above)
  • convert line delimiters to Windows, UNIX, or Mac

You can access the Cleanup option via Source->Cleanup

image Running Cleanup on my already pretty clean XML file turned the last <item> into this:

image

The first option compressed the <item></item> declaration to simply <item />. Neat, isn't it?

Final Words

+ Clean is good.

+ Uniform is good.

+ Use Ctrl-Shift-F and make it a habit.

+ Create a keyboard shortcut to the Cleanup function by going to Window->Preferences->General->Keys and use it instead of Ctrl-Shift-F (since it already formats as part of the Cleanup).

? I'd like to know if there is a way to apply auto formatting upon save, automatically. Do you?

  • http://www.planetandroid.com Ed Burnette

    There is an option to perform format/cleanup on save. I'm not in front of a computer right now but its there somewhere.

  • http://beerpla.net Artem Russakovskii

    @Ed
    I'm curious where this option is. I know there's one for Java but where's there one for XML?

    • atrey

      Aparently they didn't make one for XML yet (2012 here), but You can get this option after installing android APT for eclipse. Then there is that option available (in android preferences for XML) but who would install android plugin just for that...
      Anyway, have a nice day

      • atrey

        lol, i didn't notice this article is for android plugin xD
        preference->android->editors
        (I guess You know this already, but they put it in such a strange place so I'm just writting it here for others)

  • http://roadtoadc.blogspot.com/ TomTasche

    Ctrl + A and then Ctrl + I formats your code too :)

    Tom

    • enigma

      With no text selected the hotkey for formatting is:
      CTRL + SHIFT + F

      • http://beerpla.net Artem Russakovskii

        Yeah, this was mentioned a few times in the article...

  • http://cgctechs.com yudha

    thx for information

  • Ori

    Thanks! that realy helped me a lot!

  • http://cornerofseven.com alaQ

    For source:

    Under Window-> Preferences, go to Java->Editor->Save Actions, and enable the 'Format source code' option.

    I'm not sure if there's a similar capability for the XML, though.

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

      Haven't found one yet.

  • Whitefriars

    Thanks heaps! Just what I was looking for, why is it not the default setup?????? It turns a mishmash into readable code.

  • ossama

    thx !!

  • http://www.shabbyshackles.com/ JtotheL

    Exactly what I was looking for. Thanks!

    Using the current version of Eclipse, you can even set the key binding as "clean up" and then it doesn't even provide that prompt anymore.

  • Me

    thanks. this was useful

  • Drew

    Under Android --> Editors at the bottom is "Format on Save" which you can use instead of maually doing Ctrl+Shift_F

  • Prem…!?!

    useful...!?!

  • Brian Hultin

    <3

  • Ola Vikholt

    Do you know how to keep the />'s on their own lines to be able to easily move lines and attributes up and down?

  • FGroup Indonesia

    now this is great :D

Quantcast