From what I understand, isn't XML used for layouts and to setup how an activity looks?
My book says that XML files are converted into Java code but then, why not just write everything in Java?
From what I understand, isn't XML used for layouts and to setup how an activity looks? My book says that XML files are converted into Java code but then, why not just write everything in Java? |
|||||||||||||||||
|
Its because its simpler - tools can be written to manipulate a XML document far easier than understand java code, so the layout can be created and modified by a simple tool that does not need to also be a java parser. Its also easier for people to describe a layout in XML than in java directly. This technique is used by a lot of things, eg WSDL that describes a web service interface and is converted to (quite complex) code by a specialist tool. It helps the developer focus on one aspect without worrying about the implementation and allows tools to be written to generate different types of code (eg the wsdl can be turned into a server stub, and also a client API) |
|||||||||||||
|
Because Android designers decided to implement it that way :) In principle, everything could be written in Java. Microsoft did it for WinForms: the form description is saved as the auto-generated *.designer.cs file (or a generated region in early version of .NET framework). Each method has its pros and cons. By storing the UI as XML, it may be easier to parse so the designer can be simpler to implement. However, it is another language for developer to learn: a totally new Domain Specific Language (UI), not just XML. By storing the UI as the target language (Java, C#...), the designer's implementation may be more complex but the code for creating UI is already familiar to the developers. Another advantage is that the existing code refactor tools can work without any changes. |
|||
|