1

I've simple dialog with only one ImageView, and im using it frequently in my app, and Im wondering what is faster to load and showup, create layout in XML or make the same thing in code.

EDIT

I ask about theoretical knowledge in this point, and I cant find any useful info about that on Android Dev sites.

  • 1
    I ask about theoretical knowledge in this point. – user2913138 Oct 27 '13 at 14:21
  • I kind of wondering the same thing. Or actually more if you should use XML rather than making it in the code – starcorn Oct 27 '13 at 14:22
  • 2
    There is only ever one answer to this question with 3 parts. a. In 99% of apps, it will make no difference. b. Don't optimise unless you need to. Maintainability and readability always come first. c. If you want to know - measure it! (personally, I can't see how inflating XML can be quicker since XML is such an inefficient data storage mechanism). – Simon Oct 27 '13 at 14:28
  • Android's resource XML files are compiled into binaries that are small and fast to process. http://tools.android.com/recent/binaryxmleditor – tiguchi Oct 27 '13 at 15:30
  • @user2913138 please see my answer. It should give you some good info – superuser Oct 27 '13 at 16:40

4 Answers4

1

Theoretically in my opinion, coding the layout is faster than inflating it from xml, because inflaters need to map the layout by it self, and link id's to each other, but in code you will give the viewgroup (lets say) the reference of the view it self, there is no need to lookup it in the xml.


don't forget the step of converting the xml into java elements (views)

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
0

No difference, but if you have only one imageview make this layout in code

Jakub Pomykała
  • 2,082
  • 3
  • 27
  • 58
0

For performance, there is no noticeable difference.

But for maintainability you want to separate functionality and layout, hence it is recommended to use the layout XMLs.

Terry
  • 14,529
  • 13
  • 63
  • 88
0

No one is necessarily faster.

It will be the easiest to define the layouts in xml though, and the xml option is there as a way not to have to define buttons and ui components in java.

Also, if you define views in java, you will end up adding them to your blank xml layout's viewgroup

( ei. adding your button to the layout)

superuser
  • 731
  • 10
  • 28