0

Possible Duplicate:
Best practices: Layouts on Android (Programmatic vs XML)

Which of way of creating complex view in android is faster

1) defining the layout in xml by adding standard view in layout.

2) Draw the required in the onDraw of an custom view

Community
  • 1
  • 1
Sandeep Dhull
  • 1,638
  • 2
  • 18
  • 30

2 Answers2

4

If you are talking about performance, it depends on how efficient you are with your onDraw().

When you define a view in XML, all you are doing is telling the layout inflater to create an instance of the view type class and set it's initial properties to the XML attributes you define. Then, when the view is drawn, guess what - it's onDraw() is called to draw the view.

So, if you extended the View and overrode the onDraw() and used exactly the same method to draw the view as it's super class would use, then there is no performance difference.

So, the answer (if indeed you are asking about performance) is "it depends".

Likewise, just as you could be inefficient in your onDraw(), you could be inefficient in your layout design. Redundant view groups, like nested LinearLayouts, RelativeLayouts etc can have a very big negative impact on performance. The layout hierarchy viewer is a great tool to get to know.

Simon
  • 14,407
  • 8
  • 46
  • 61
2

Good answer from Ollie C https://stackoverflow.com/a/9827887/603233:

The advantages of XML are:

 1. Ability to use layout editors (Eclipse)
 2. Easier to preview layouts
 3. Possible to benefit from auto-localisation of layouts
 4. Easily maintain different parallel layouts for difference devices (screens)
 5. Can get a sense of the layout by looking at it (easier than code)
 6. Easy to break layouts down into pieces (fragments, includes, etc) to remove duplication
 7. Keeps a separation between the visual design, and the functionality behind it
Community
  • 1
  • 1
kumar_android
  • 2,273
  • 1
  • 21
  • 30