Android: The mystery of ConstraintLayout
Recently I was working on a simple login layout. Nothing really special a ConstraintLayout
which has LinearLayout
, a few Edit Text
, Text View
and Button
. A very common use case for most applications nowadays. So I took the first EditText
from the palette and dropped it into the ConstraintLayout
connecting it’s constrained and setting the width to 0dp
<TextView
android:id="@+id/text_view_application_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_normal"
android:layout_marginEnd="@dimen/margin_normal"
android:layout_marginBottom="@dimen/margin_normal"
android:text="@string/text_application_name"
android:textAlignment="center"
android:textSize="@dimen/size_text_title"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Looks pretty standard right? But let’s pay attention to the lines below
<TextView
...
android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
From the official Google documentation
we can read that ConstrainLayout
size can be adjusted as follows
- Fixed: You specify a specific dimension in the text box below or by resizing the view in the editor.
- Wrap Content: The view expands only as much as needed to fit its contents.
- Match Constraints: The view expands as much as possible to meet the constraints on each side (after accounting for the view’s margins). However, you can modify that behavior with the following attributes and values (these attributes take effect only when you set the view width to match constraints)
And just after this, we are reading following warring
Note: You cannot use
match_parent
for any view in aConstraintLayout
. Instead use "match constraints" (0dp
).
Interesting, so Google is not allowing us to use match_parent
, but what if I set match_parent
instead of 0dp
and remove constraints to end
and start
, will I get Lint errors, will my UI look broken on the devices, what will happen 🤔?
<TextView
...
android:layout_width="match_parent"
a̶p̶p̶:̶l̶a̶y̶o̶u̶t̶_̶c̶o̶n̶s̶t̶r̶a̶i̶n̶t̶E̶n̶d̶_̶t̶o̶E̶n̶d̶O̶f̶=̶"̶p̶a̶r̶e̶n̶t̶"̶
a̶p̶p̶:̶l̶a̶y̶o̶u̶t̶_̶c̶o̶n̶s̶t̶r̶a̶i̶n̶t̶S̶t̶a̶r̶t̶_̶t̶o̶S̶t̶a̶r̶t̶O̶f̶=̶"̶p̶a̶r̶e̶n̶t̶"̶ />
For now, I can only see an error in the layout editor
but when I run the application on the phone UI is not broken and Lint is also not complaining about the match_parent
or missing constrains.
Okay if everything works fine, then maybe there is some performance impact when using match_parent
, I thought. I found a very good article about ConstrainLayout
performance
In the article 👆 you can find source code, to check your layout performance, I checked out the source code, modified it a bit and run the performance tests, for ConstraintLayout
with match_parent
and with 0dp
and constraints. The results were pretty interesting
I used the latest version of ConstraintLayout 2.0.0.
Each test was run 1 000 times and shown results are the average of them, as you can see the view without constraints is rendered faster than the one with constraints, which is a bit surprising as it’s not a recommended way of using it.
Maybe Nicolas Roard can help, to understand how this works and why I should not use the match_parent
even if the performance is a bit better?
If someone knows the answer please leave it in the comments 🥰