I have been struggling with a nasty ANR problem with my app, which loads a SurfaceView. Originally I created it Java-wise in the Activity:
mSurfaceView msv = new mSurfaceView(this);
//set params etc.
myLayout.add(msv);
I then decided to switch to XML and lay out everything in an XML file and then just findViewById(…), and the ANR seems to have gone.
My questions is: is XML loading faster than with Java?
The XML files are never read at runtime. They are converted to a binary format when compiling. I assume there are some optimizations done during this conversion which makes inflating the layouts relatively fast. I would imagine XML layouts are faster than doing it in code. XML layouts and programming the layout in code are not one to one, I’ve noticed bugs sometimes with XML that were solved by doing the layout in the Java code.