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.
Labels:
android,
eclipse,
readability,
XML
Subscribe to:
Post Comments (Atom)
hi, my R file appears to go missing. My XML code appears to be fine with closing tags etc. I'm wondering has it anything to do with cleaning my project or when I build it! thanks
ReplyDeleteHi Michelle,
DeleteIn general R.java isn't generated if there is an error or a bug in your XML. Check your any XML files added or edited for errors. When your sure that there are no errors go to Project>Clean to rebuild the project.
Hope this helps.