I know that there are differences in what you can and can not do in XML and a java file, but so far I understood that a java file can do everything a XML file can do, at least that's written in the book I am reading (see the quotes below).
My question is therefor if there can be a performance difference in writing your code in java or xml, and if it is: which of the two is favored over the other? This is not a question about the correct way to do this, it's a question about pure performance.
I know that the xml code is compiled into java code, maybe this can be done more efficient than translating java code since it would be easier to write a compiler for xml than for java files? (based on the assumption that indeed xml is easier to parse since it's for example more coherent and the structure is simpler)
Quote from the book
This is the actual quote from the book which states that a java file can do everything an xml file can do. It does so by first explaining that there are two ways to make User Interfaces, namely in a Declarative and Programmatic way:
The declarative approach involves using XML to declare what the UI will look like, ...
...
A programmatic user interface involves writing Java code to develop the UI.
...
Everything you can do declaratively, you can also do programmatically.
Yes, you can. Just call "setContentView" with the same xml and it will use the same layout. Change whatever you need to programmatically during runtime.
Yes you can create a layout without XML. You can do something like extending from LinearLayout as a new class and then in the onCreate of your Activity you would pass in that view into setContentView instead of the layout reference.
Android Layout TypesTableLayout is a view that groups views into rows and columns. AbsoluteLayout enables you to specify the exact location of its children. The FrameLayout is a placeholder on screen that you can use to display a single view. ListView is a view group that displays a list of scrollable items.
Android provides a very flexible way to display layouts using XML-based layouts. This helps us create apps while keeping the layout and logic completely separate. This makes it very easy for us to change the layout even after the application is written, without having any changes in the programming logic.
creating the ui hierarchy by hand in your activity class will be faster but MUCH more complicated than using xml layout files (about the LayoutInflater performance see its createView(String name, String prefix, AttributeSet attrs)
method used when inflating the layouts and you will find out its not a speed daemon), i cannot give you any numbers in % though
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With