Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could there be any performance difference between writing the same code (for the layout) in XML or java file?

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.

like image 511
Joop Avatar asked Apr 09 '15 07:04

Joop


People also ask

Can you use a same XML layout for 2 different activities?

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.

Is it possible to design layout in Java instead of XML?

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.

What are the different layouts used in XML?

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.

What is the importance of XML-based layouts?

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.


1 Answers

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

like image 104
pskink Avatar answered Oct 12 '22 17:10

pskink