Showing posts with label readability. Show all posts
Showing posts with label readability. Show all posts

Monday, 10 February 2014

Android XML formatting in Eclipse


When working with Android in Eclipse XML errors can be easy to commit and hard to spot. Errors in your XML can lead to your code failing to compile and the auto generated files (e.g. R.java) disappearing.
Proper formatting can make it easier to spot errors like unclosed tags etc.
Take the following code as an example:

Multiple attributes on a single line, a mix of self closing and empty tags (line 15) make this code harder then it should be to read. Fortunately Eclipse has built in preferences which can help you.

In Window>Preferences>XML>XML Files> Editor


Ensure the tick is placed on Split multiple attributes each on a new line.
Ensure Insert whitespace before closing empty end-tags is ticked.
Apply and OK.
Source>Format:

Much better, right? But there's more:
Source> Cleanup Document:


Here you can do the above magic. Press OK and the code looks much cleaner and easier to read:



Simples.