Search code examples
androidandroid-studioandroid-relativelayoutandroid-constraintlayout

Is constraint layout more flexible in locating views in Android Studio?


Both constraint and relative view groups support relative positioning of a view wrt other. Relative layout provides 4 different attributes : layout_toRightOf / toLeftOf / toTopOf / toBottomOf , and Constraint provides many combinations of format " layout_constraintTop_toTopOf "

BUT can’t we place the views at any position just using the 4 attributes of Relative layout ? In what way is Constraint layout more responsive ?


Solution

  • According to Google Developers ConstraintLayout has better performance than RelativeLayout. In addition (as you've mentioned) it's more advanced than RelativeLayout and allows you to build your Layout more precisely with all these additional constraints options. What's more, if you set the view A's width or height to match_constraint (0dp), and set its end constraint to start of view B, the A's width or height can be dynamically scaled when B's visibility is gone.

    Any view inside ConstraintLayout requires only two constraints for X and Y axis e.g. layout_constraintTop_toTopOf and layout_constraintStart_toStartOf.