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.

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.