3

I was ready some articles about performance optimization for android layouts. Most of them recommended using RelativeLayouts over other layouts because this might help you avoid Nested Layouts, that consume memory.

Personally, I believe that every thing has its Advantages and Disadvantages. But I could not figure out the disadvantages of the RelativeLayouts over the different types of layouts.

What is the disadvantages of RelativeLayouts?

When I should avoid using RelativeLayouts?

Thanks in advance.

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • AFAIK there's no disadvantage of using RelativeLayout! Rather I'd say it's better among all layouts, you can position your components at the desired position. – Apurva Mar 22 '15 at 07:03
  • I believe there is, at least this link saying that **Relative layouts include an additional pass for measuring, but it is recommended** ... http://blog.echolocker.com/performance – Sami Eltamawy Mar 22 '15 at 07:06

2 Answers2

3

Relative Layout is the most used layout in most cases and from my experience, no disadvantage using this layout.Like I said before pick whichever is the best for the job, and worry about performance later.

update :

I copied comment from 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

https://developers.google.com/events/io/sessions/325615129

Community
  • 1
  • 1
  • Thanks for those general info. But I know all that. I'm asking about the mechanism of `RelativeLayout` and the cases that using it is not recommended. – Sami Eltamawy Mar 22 '15 at 07:35
  • 1
    Relative Layout is the most used layout in most cases and from my experience, no disadvantage using this layout.Like I said before pick whichever is the best for the job, and worry about performance later. if you really-really worried about the disadvantage, performance, & etc.. you can read http://stackoverflow.com/questions/4069037/android-is-a-relativelayout-more-expensive-than-a-linearlayout – R. Irfan Pohan Mar 22 '15 at 07:58
  • This is a useful link. Modify your answer and add it into it with some description for people. – Sami Eltamawy Mar 22 '15 at 08:16
3

For me, RelativeLayout is too much time consuming if you have to reorganize your components. That is the biggest downside.

For this reason, I would say RelativeLayout are really good as top level layout, and bottom level layout. But the mid-level layouts, are better served using the taylored (Linear, Table...) layout.

For example, when creating a form, the very top layout of my Activity or Fragment will be a RelativeLayout, but my form will be created as one big vertical LinearLayout. And inside this Linear, each line will be a RelativeLayout in which I will have a Text View and an Edit Text.

This way I can very easily sort the fields of my form and (I think) I keep my layout memory friendly by not overusing nested LinearLayout.

JDenais
  • 2,956
  • 2
  • 21
  • 30