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?
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.
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.
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.
More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With