Auto Formating Android XML with Eclipse

Wednesday, September 29, 2010

Hello Android Lovers, In this article will explain the problem with the handling of Eclipse on the XML file format and the best way to fix it.

People using Eclipse for their Android development for several reasons:

    * it’s the only IDE fully supported by the Android dev team
    * it has a visual Layout/Resources builder that transforms XML files 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

Problem

However, one thing about the Eclipse Android development has been disturbing for some time – and it is an XML format by visual tools. If you open the XML file in Eclipse and using this Layout / Resources tab (which serves both as a visual UI builder or a visual interface for resource management), Eclipse creates an XML representation of what you are building and sewage into the files you edit. The problem is that XML is not formatted in any way – Eclipse only write on one line, which looks really horrible and basically unusable.

For example, we have the XML file is formatted with beautiful Android sample menus. After using the Layout tab to add other items, ending with a single line unindented bad for it.
\

The Solution

Here’s how to solve this problem in a way that is almost completely automated, using formatting rules that can generate the most readable file. There is no need for external devices – Eclipse provides all the needs.

go to Window-> Preferences-> an XML-> XML File-> Editor


















check out some of the “Split each attribute on a new line” and adjust the other options according to your wishes (such as Indent Indent using spaces and sizes)
press OK to save options
you have to do now is to press Ctrl-Shift-F or select Source-> Format
Let’s see what my XML is not to show above:


 Eclipse’s Auto Format feature is one of the best (yes, you can apply it to the Android Java code as well and nearly all other document formats).
Bonus – XML Cleanup
As an added bonus, the Eclipse also offers a choice of cleaning XML, which can do the following:
compress an empty element tag
insert required attributes
insert missing tags
Quote attribute values
source format (as above)
lines to convert to Windows, UNIX, or Mac
You can access the Cleanup option via Source-> Clean Up


Running Cleanup on we already pretty clean XML file turned the last into this:



The first option compressed the declaration to simply . 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).
source: homeitinnovation.com

0 comments:

Post a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))