Constraint Layout performance

Maciej Kozłowski
AndroidPub
Published in
3 min readNov 28, 2018

My last article was about developers’ mistakes related with communication between Activities and Fragment. Today I’d like to keep mistakes’ topic, but focus on creating layouts.

Many of developers use Constraint Layout in each possible view, but this is a mistake in a few aspects. In this article I will compare Constraint Layout with others layouts and show you when you’d better use specific layout.

Tests

I’ve prepared a few examples using Constraint Layout in version 1.1.3. Each test was run 1 000 times and shown results are average of them. You can check my code here.
Firstly, I will show you how the layout looks like. Blue stroke line shows size of layout. Then I will show and compare performance results.

  1. View with 3 child views which are under each other.

This is typical use case of Linear Layout. As you can see in the chart Linear Layout is faster. My point of this example is that layouts which are created to resolve specific cases are better than more general layouts like Constraint Layout.

2. View centered in another view.

If we have to use that view, the best option is FrameLayout. This example also shows that if we can use the simplest layout it’s the best option for our performance.

3. Layout with 2 views on different sides.

Results show that the fastest layout is Relative Layout, but difference between this and Linear Layout is really small, what we can’t say about Constraint Layout.

4. View with more complex hierarchy of view

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

I want to pay attention to 2 more things which are more subjective.

  • Creating Constraint Layout takes more time than other layouts. Also introducing changes in existing Constraint Layout is much more time-consuming.
  • Moreover in my personal opinion xml file of Constraint Layout is much less readable and clear than other layouts.

I believe we shouldn’t make our development more complex and harder if it doesn’t bring us any advantages.

Let’s keep our code simple and clear, as long as we can.

Summary

My tests show that Constraint Layout is not so good. There are a few aspects which cause we should use different layouts. I’m sure that Google developers are still working on performance of Constraint Layout, but for now we should think twice before introducing this layout to our projects.

Check out also my latest article about Security in Android.

If you find out something useful from my articles, give me 👏🏼 and follow me.

--

--

Maciej Kozłowski
AndroidPub

Senior Android Developer. Kotlin lover. Tech stack: MVVM, Dagger2, Coroutines, Flow, Compose, Android Architecture Components