I have a requirement where different views are to be shown on the same screen depending on the requirement. Which approach is better adding respective XML files or writing in java code by using TabActivity
class object?

- 1,664
- 19
- 35

- 47
- 2
- 9
-
1Does this answer your question? [Could there be any performance difference between writing the same code (for the layout) in XML or java file?](https://stackoverflow.com/questions/29532974/could-there-be-any-performance-difference-between-writing-the-same-code-for-the) – Vinay Jayaram Dec 02 '19 at 05:36
3 Answers
Short answer, defining your layout in code is better for performance than using XML
.
Inflating an XML
layout involves doing the same work as creating the layout in code, however you also need to parse the layout file (at least the first time it's used) which adds work compared to doing it in code.
HOWEVER, for the majority of use cases the performance hit is not noticeable, and the simplicity of defining an XML layout outweighs the performance benefits of doing it in code.

- 2,113
- 2
- 21
- 35
IMO, Writing layouts file is a better option.
Since, Writing layout will let the view load on compile time.
Whereas, While creating views pro-grammatically leads to slight increase in Runtime operations of the app.

- 6,907
- 2
- 24
- 35
Code written in java means dynamic generation of views are faster than xml rendering as the views increasing. Xml prefers as it is bit easy to implement the design using xml but it gets tough to handle when we use java to create dynamic views.
In your case if you have requirements to create dynamic view then you can create with Java.

- 269
- 1
- 11