constraintlayout icon indicating copy to clipboard operation
constraintlayout copied to clipboard

Documentation: explain why match_parent is not recommended inside ConstraintLayout

Open johnlinp opened this issue 3 years ago • 2 comments

In the developer guide: https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout, there is a note:

Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

It suggests that match_parent is not recommended inside ConstraintLayout, and it also suggests an alternative. However, it didn't explain why it is not recommended. The lack of explanation causes some questions like https://stackoverflow.com/questions/59237466/how-come-youre-not-suppose-to-use-match-parent-in-constraint-layout and https://github.com/google-developer-training/android-basics-kotlin-tip-calculator-app-solution/issues/34.

It would be better to explain why match_parent is not recommended inside ConstraintLayout. Thanks.

johnlinp avatar May 25 '21 02:05 johnlinp

MATCH_PARENT is there because the underlying definition of layout_width/height has it.

MATCH_PARENTs behavior can be confusing because how it should behave in many situations is not obvious and has no clearly correct answer. I would say if you are only using the most straight forward definition of "fill the parent in that axis" then it is clear what you mean by match_parent. if you are adding constraints and margins on that axis use 0dp and Constrain it.

If we recommended it we would have a long section of the developer guide explaining its behavior and we would still have a many confused people.

jafu888 avatar May 25 '21 05:05 jafu888

@jafu888 Thank you for the explanations. In fact, I created this issue just to see these explanations. Can we put these explanation in the developer guide?

johnlinp avatar Jun 08 '21 03:06 johnlinp