Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout vs RelativeLayout performance 2020

I am creating complex layouts with many Views on various screens of my app.The documentation says that:

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.

So an alternative is to use Relative Layout or Constraint layout. This late 2018 article says Relative layout is better.

Today in terms of performance which one is better? Relative layout or Constraint layout?

like image 646
GTR - Vitor Avatar asked Jan 07 '20 12:01

GTR - Vitor


People also ask

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.

Which of the LinearLayout ConstraintLayout and RelativeLayout performs best?

ConstraintLayout has a better performance since it's introduced. A performance benchmark of ConstraintLayout vs RelativeLayout is available in this official blog post by Google - ConstraintLayout is ~40% faster.

Which has better performance LinearLayout or RelativeLayout?

So Linear Layout should preferred over Relative Layout! Also if you use weight_sum attribute within Linear Layout it would again “measure the child twice”. So avoid using this attribute as much as possible.

Is ConstraintLayout faster than LinearLayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.


2 Answers

I would use the constraint layout. It shall perform better.

In the N release of Android, the ConstraintLayout class provides similar functionality to RelativeLayout, but at a significantly lower cost.

Quote from performance/rendering/optimizing-view-hierarchies at the bottom

like image 80
Vall0n Avatar answered Nov 11 '22 04:11

Vall0n


Note that the late 2018 article referenced in the question discusses the performance of RelativeLayout vs nested LinearLayouts and doesn't mention ConstraintLayout. ConstraintLayout has a better performance since it's introduced. A performance benchmark of ConstraintLayout vs RelativeLayout is available in this official blog post by Google - ConstraintLayout is ~40% faster.

like image 35
Max Poon Avatar answered Nov 11 '22 04:11

Max Poon