1

After developing few applications, I know that many people like to add view programmatic ally.

But is it true that all XML layouts can be converted to Java code dynamically?

What are the pros and cons of using Java code to generate the layout?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
jjLin
  • 3,281
  • 8
  • 32
  • 55
  • possible duplicate of [Dynamic vs XML layout in Android?](http://stackoverflow.com/questions/11960501/dynamic-vs-xml-layout-in-android) – Selvin Apr 11 '13 at 13:50

3 Answers3

2

The layout XML you are creating is being translated at run-time to java code. So the answer to your question is Yes. You can do the same and basically create all your layout in java code from scratch.

The obvious down grade of this thehnic as you know is that it will take you much more time to achieve the same result as with the XML file.

So, unless you have to add view dynamically to you layout, I don't see a reason for you doing that process using java code.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
1

But is it all xml layout can convert to java code dynamically?

Yes

What is the pros and cons of using java code to generate the layout?

According to the documentation 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.


From Hello Android by Ed Burnette:
User interfaces can be designed using one of two methods: procedural and declarative. Procedural simply means in code. For example, when you’re programming a Swing application, you write Java code to create and manipulate all the user interface objects such as JFrame and JButton. Thus, Swing is procedural.

Declarative design, on the other hand, does not involve any code. When you’re designing a simple web page, you use HTML, a markup language similar to XML that describes what you want to see on the page, not how you want to do it. HTML is declarative.

Android tries to straddle the gap between the procedural and declarative worlds by letting you create user interfaces in either style. You can stay almost entirely in Java code, or you can stay almost entirely in XML descriptors. If you look up the documentation for any Android user interface component, you’ll see both the Java APIs and the corresponding declarative XML attributes that do the same thing.

Which should you use? Either way is valid, but Google’s advice is to use declarative XML as much as possible. The XML code is often shorter and easier to understand than the corresponding Java code, and it’s less likely to change in future versions.

Jainendra
  • 24,713
  • 30
  • 122
  • 169
0

Pretty much everything that you can do in the XML can be done programmatically. I think it gets very difficult when it comes to drawable shapes and such. I for one only do layout stuff programmatically when I don't know beforehand what I need to add.

Example: I have some Articles that can have 1- x descriptions, I don't know. Then I programmatically create TextViews that I add to a layout.

fweigl
  • 21,278
  • 20
  • 114
  • 205