Java – Why use XML in Android

androidandroid-developmentjavaxml

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?

Best Answer

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)