Is a RelativeLayout more expensive than a LinearLayout?

nbarraille picture nbarraille · Nov 1, 2010 · Viewed 46.1k times · Source

I've always been using RelativeLayout everytime I needed a View container, because of it's flexibility, even if I just wanted to display something really simple.

Is it ok to do so, or should I try using a LinearLayout when I can, from a performance/good practices standpoint?

Thanks!

Answer

Pierre-Antoine LaFayette picture Pierre-Antoine LaFayette · Jul 5, 2013

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