Is a Relativelayout More Expensive Than a Linearlayout

Is a RelativeLayout more expensive than a LinearLayout?

In a talk at Google I/O 2013 (Writing Custom Views for Android), Romain Guy clarified the misunderstanding that caused everyone to start using RelativeLayouts for everything. A RelativeLayout always has to do two measure passes. Overall it is negligible as long as your view hierarchy is simple. But if your hierarchy is complex, doing an extra measure pass could potentially be fairly costly. Also if you nest RelativeLayouts, you get an exponential measurement algorithm.

https://www.youtube.com/watch?v=NYtB6mlu7vA&t=1m41s

https://www.youtube.com/watch?v=NYtB6mlu7vA&t=38m04s

Which layout is better in terms of Performance in Android? RelativeLayout or Linear Layout?

RelativeLayout is measured twice, so LinearLayout has better performance when used right.

RelativeLayout vs nested Linear Layout performance

That's a very broad question and there's no single, simple answer. LinearLayout is generally simpler (and therefore faster) than RelativeLayout, but LinearLayout has a problematic case if you nest multiple of them inside each other, with weights on the same axis. Then it has to iteratively divide up the space and this takes lots of layout passes (it's so bad there's a lint warning against this).

Even when you avoid that case, then with nested LinearLayouts you will still have a deeper view hierarchy compared to using RelativeLayout, so while LinearLayout is faster, that balances out at some point.

So it becomes the same thing as with all things performance: the only way to be absolutely sure is to measure and see what happens.

RelativeLayout vs LinearLayout with weights

It actually depends on what you are trying to implement. If this is just a content of a row from ListView, I would prefer LinearLayout with weights. There are good answers from this question about using RelativeLayout or LinearLayout, which is related to your's:

Is a RelativeLayout more expensive than a LinearLayout?

LinearLayout vs RelativeLayout

Read this article:

The Android UI toolkit offers several layout managers that are rather easy to use and, most of the time, you only need the basic features of these layout managers to implement a user interface. Sticking to the basic features is unfortunately not the most efficient way to create user interfaces. A common example is the abuse of LinearLayout, which leads to a proliferation of views in the view hierarchy. Every view, or worse every layout manager, you add to your application comes at a cost: initialization, layout and drawing become slower. The layout pass can be especially expensive when you nest several LinearLayout that use the weight parameter, which requires the child to be measured twice...

In a RelativeLayout, views are aligned either with their parent, the RelativeLayout itself, or other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout and that the title is positioned above the description and anchored to the parent's top. With the description GONE, RelativeLayout doesn't know where to position the title's bottom edge. To solve this problem, you can use a very special layout parameter called alignWithParentIfMissing.

This boolean parameter simply tells RelativeLayout to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing to true, RelativeLayout will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing will cause RelativeLayout to align the title's bottom with its own bottom.

...the difference will be much more important when you use such a layout for every item in a ListView for instance...



Related Topics



Leave a reply



Submit