Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
1  
Check out this link on how XML comes in handy over Java in layout. – daumie Jan 19 '16 at 9:56
1  
XML has about two syntactic elements. Java has... a lot more. – Kilian Foth Jan 19 '16 at 10:03
    
If you really want a good understanding of why, try writing even a simple Android app without XML. While possible, it would definitely be frustrating. – David Etler Jan 19 '16 at 13:29
    
In a sense, XML used in this way is like an interpretted language and Java is a functional language. In one case, you're writing code that does the task and in the other, you provide all information necessary to do it without the how. In Java, you can only run the code while with XML, you can decide later to optimize rendering without changing the overall appearance. It is a tremendous advantage that shouldn't be overlooked. – Neil Jan 19 '16 at 16:05

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)

share|improve this answer
    
To agree and expand a bit - by "simpler" we generally mean a higher level of abstraction - it will (should!) take less XML to describe the desired result than it would take Java (or similarly XAML and C#) because the XML is in effect a domain specific language for layout etc. This should also mean that its easier to understand the intent i.e. layout from the XML than from the equivalent java code. – Murph Jan 19 '16 at 13:01
    
Microsoft took a similar approach with their UI related stuff in the form of XAML. – lzcd Jan 20 '16 at 23:18
    
@lzcd as do nearly all others - Qt has its QML, even MFC has a .rc file that describes a UI layout in plain text. – gbjbaanb Jan 21 '16 at 8:33

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.