rogerstone
rogerstone

Reputation: 7671

Why is XML used for the creation of UI layouts in Android?

I would like to know why we use XML for the creating user interface layouts in Android. I know that it decouples the business logic from the design but what is the significance of the XML other than that?

Also I would like to know the significance of the auto-generated R.java file in this. All I know that it is generated according to the changes in the resources and that it helps us to access the widgets and resources through their IDs.

It would be great if someone could give a clear idea on these two aspects.

Upvotes: 26

Views: 20425

Answers (6)

rogerstone
rogerstone

Reputation: 7671

Unlike what everyone said about the XML being easy and efficient. Here is what I read in Hello Android by Ed Brunnette (p. 49) which made sense.

Android is optimized for mobile devices with limited memory and horsepower, so you may find it strange that it uses XML so pervasively. After all, XML is a verbose, human-readable format not known for its brevity or efficiency, right?

Although you see XML when writing your program, the Eclipse plug-in invokes the Android resource compiler, aapt, to preprocess the XML into a compressed binary format.**It is this format, not the original XML text, that is stored on the device.

This was the kind of answer that i was looking for.(sorry if my question meant otherwise).

The reason that XML was chosen is mainly because of its familiarity and the number of IDE tools that natively support it. The developers could have chosen JSON for example and still compiled that to binary.The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource.

Upvotes: 38

Hitesh Sahu
Hitesh Sahu

Reputation: 45160

Android Layouts are tree like structures with some enforced rules. XMLs are perfect fit for this purpose.

JSON also have tree like structure but they are data-oriented while XML is document-oriented.** :

Meaning XML is based on the idea that documents have structural and other semantic elements that can be described without reference to how such elements should be displayed.

The actual display of such a document may vary, depending on the output medium and style preferences.

While JSON was designed to represent JavaScript and their prime purpose is data exchange . They are well suited in data-oriented areas because of light weight and simplicity & closer in syntactic form to programming data structures.

Upvotes: 0

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. Also, you have already mentioned the decoupling.

As for R.java - it is just the way of accessing widgets. The solution is rather good, as it is using ints instead of string to use less memory and in the same time provides well readable names for the simplicity of development.

Upvotes: 1

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54820

XML is easy to parse and manipulate programmatically, it's basically a tree structure and most UI creation tools already use it. It really has nothing to do with decoupling business logic because you can define Java code in Android using a Model-View-Controller pattern just as well.

The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource. It also stops you from making stupid mistakes since the compiler will complain if you try to access a resource you haven't defined. If you were using a simple properties file you wouldn't know until runtime that the 'key' you are using is missing.

Upvotes: 14

IanNorton
IanNorton

Reputation: 7282

One possible reason is that you need not have any working java underneath in order to be able to see the visual layout of the interface you are working on. The xml ui element/page is essentially a document that you can parse and display. If this were a source file you would have to either carefully parse it or compile and run it (all of which are more complex than parsing xml)

Upvotes: 1

PedroAGSantos
PedroAGSantos

Reputation: 2346

Same as why is silverlight with xml the answer is simple xml give power by integration and scalability. R.java is for indexing having things organized is never bad. Sorry my english

Upvotes: 3

Related Questions