Is a RelativeLayout more expensive than a LinearLayout?
Asked Answered
T

5

116

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!

Truckload answered 1/11, 2010 at 12:55 Comment(0)
G
150

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

Grandma answered 5/7, 2013 at 20:14 Comment(3)
I know this is an old post, but what would you consider to be the least costly solution when choosing between a single relativelayout and 2-3 linearlayouts?Warrington
A ConstraintLayout :)Whorish
So you're saying a RelativeLayout is better than LinearLayout in performance?Pantywaist
M
53

Unless you're laying out lots of Views (e.g. in a ListView), the performance of choosing between LinearLayout or RelativeLayout is negligible. Pick whichever is most convenient to use for the job, and worry about performance only when you need to.

And here's what the official docs about Creating Efficient Layouts says about performance of RelativeLayout and LinearLayout:

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 — that 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.

Magisterial answered 1/11, 2010 at 13:0 Comment(2)
the justification to it being negligible ? i found this which states relativeLayout costs more just as i suspected bitbucket.org/spencerelliott/mercury/issue/1/…Kathiekathleen
Just avoid adding Inner containers.Allstar
B
2

Relativelayout is more effective than Linearlayout.

From here:

It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing. For example, using nested instances of LinearLayout can lead to an excessively deep view hierarchy. Furthermore, nesting several instances of LinearLayout that use the layout_weight parameter can be especially expensive as each child needs to be measured twice. This is particularly important when the layout is inflated repeatedly, such as when used in a ListView or GridView.

Bennion answered 2/3, 2015 at 8:5 Comment(3)
@phreakhead You're right that there's no single "correct" solution. Which is why the "correct" solution is almost always to forget about performance entirely and do whatever is easiest to write and read.Floridafloridia
that quote does not back up your statement, which is only true in one situationEvieevil
The really truth is, design your layout always trying to minimizing the number of hierarchy levels (layout inside layout inside layout) and check the hierarchy viewer tool to find potential problems.Baltimore
N
1

2018 UPDATE: In the N release of Android, the ConstraintLayout class provides similar functionality to RelativeLayout, but at a significantly lower cost. It is very powerful layout manager and it should be used whenever it is necessary to build a complex GUI.

Nisan answered 21/4, 2018 at 11:47 Comment(2)
since the latest version it became very very slowPosey
It depends on how it is used. You should avoid absolute positioning and use constraints.Nisan
A
-26

You can try

<LinearLayout>
       <ViewPager/><!--Loading images from net, it is very good as a testing case.-->
       <ViewPagerIndicator/>
       <TextView/> <!--Show some info about page-->
</LinearLayout>

<RelativeLayout>           
       <ViewPager/><!--Loading images from net, it is very good as a testing case.-->
       <ViewPagerIndicator below="id of ViewPager"/>
       <TextView below="id of ViewPagerIndicator"/> <!--Show some info about page-->
</RelativeLayout>

You will find that there're a lot of different, if your Pages loading some images from internet. In this case the LinearLayout is 100% better than RelativeLayout ever.

Alboin answered 8/11, 2013 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.