Aswin
Aswin

Reputation: 1214

Why we use xml?

We are creating many XML files for a single app in developing android applications; of course it's the main theme of android. For every screen of app we are creating an XML with some layouts (static implementation). In execution, every time XML file will be read to get all the resources that it wants to display the screen which we designed.

The same design can be achieved by adding few lines in the java code itself (dynamic implementation). Like we can extend ListActivity instead of creating XML file, for example. Then why to go for XML as the application need to read a file from phone memory which may leads to slow access and makes application slow?

So, dynamic implementation is always better than static one.

Please correct me if I am wrong.

Upvotes: 6

Views: 4188

Answers (5)

telkins
telkins

Reputation: 10550

XML files are strongly encouraged because it makes your app more closely follow the Model-View-Controller programming strategy. Having each part as separate from each other as possible makes it easier to develop and maintain your overall program. The performance difference seems to be minimal, although others have said that XML is preprocessed.

Upvotes: 2

Cathal Comerford
Cathal Comerford

Reputation: 1020

Using XML also is a great help in that you can see exactly what each layout does, or at least what its meant contain. Whereas if your XML is just an empty LinearLayout and everything is added programatically, when you look at that XML file you'll have no clue what its used for and have to go look up the code that uses it.

Upvotes: 0

Jack Satriano
Jack Satriano

Reputation: 2029

It helps organize and visualize the layouts very nicely. It provides a backbone to the gui that allows multiple devices to be supported by much less code.

Upvotes: 6

Stefano Ortisi
Stefano Ortisi

Reputation: 5336

From Android Developer Documentation:

http://developer.android.com/guide/topics/ui/declaring-layout.html

The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile. For example, you can create XML layouts for different screen orientations, different device screen sizes, and different languages. Additionally, declaring the layout in XML makes it easier to visualize the structure of your UI, so it's easier to debug problems. As such, this document focuses on teaching you how to declare your layout in XML

There are Pros and Cons in both method (XML and runtime programming). It depends on what you want achieve. For example, if you want differentiate your presentation layout depending on screen size, screen density, language, ecc.. the XML way is the best solution.

Upvotes: 0

kosa
kosa

Reputation: 66677

Yes if your content being displayed is dynamic, java code is best choice.

There may be cases where content being displayed may not be dynamic, in that case XML will be helpful.

Another advantage with XML is, it separates presentation from logic. Read this link for more information.

The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile

Upvotes: 0

Related Questions