35

Why RelativeLayout replaced to ConstraintLayout in the default layout file of an android empty activity. I need to know why we should use ConstraintLayout, what other benefits it provides to us.

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Manish Patiyal
  • 4,427
  • 5
  • 21
  • 35
  • 44
    Because it's new. And new things are shiny – Tim Jun 23 '16 at 13:12
  • 3
    It essentially is a better `RelativeLayout`. It can do many things the `RelativeLayout` cannot and it has much better performance. – Xaver Kapeller Jun 23 '16 at 13:17
  • And of course it is a pleasure to work with. You can simplify layouts like you never could with a `RelativeLayout`. I encourage anyone to play around with it, its great. Of course don't use the graphical editor, they are always terrible. Edit the xml directly to get the most out of your layouts. – Xaver Kapeller Jun 23 '16 at 13:19
  • And empty Activity? Why would you have just a `RelativeLayout` in an activity? I assume you are attaching a Fragment there? Don't use a RelativeLayout for that... Use a FrameLayout instead. Much much better performance, especially when you have it so high up in your code hierarchy. – Xaver Kapeller Jun 23 '16 at 13:21
  • 6
    @XaverKapeller can you give me example to clarify this statement? `ConstraintLayout can do many things the RelativeLayout cannot ` –  Mar 24 '17 at 16:53

1 Answers1

27

As provided by Xaver Kapeller - "The main purpose of the ConstraintLayout is to fix problems with the RelativeLayout, and it does it so well. You can do so many things that were impossible with a RelativeLayout. And you can simplify your layout like you never could before. Additionally, it fixes a long-standing performance issue of the RelativeLayout. The double taxation during the measure/layout phase. So we get better performance, more versatility and much simpler layouts in one nice package."

An additional benefit of using it is that it allows you to create your layouts using a drag and drop style editor visually in a way RelativeLayout couldn't similar to Interface Builder in Xcode.

Constraint Layout: This new layout is a flexible layout manager for your app that allows you to create dynamic user interfaces without nesting multiple layouts. It is distributed as a support library that is tightly coupled with Android Studio and backwards compatible to API Level 9.

At first glance, Constraint Layout is similar to RelativeLayout. However, the Constraint Layout was designed to be used in Studio and it can efficiently express your app design so that you rely on fewer layouts like LinearLayout, FrameLayout, TableLayout, or GridLayout. Lastly, with the built-in automatic constraints inference engine. You can freely design your UI to your liking and let Android Studio do the hard work.

user1579019
  • 455
  • 1
  • 7
  • 19
egfconnor
  • 2,637
  • 1
  • 26
  • 44
  • 3
    can you give me any example? where the relative layout failed or take more time/code than ConstraintLayout? –  Mar 23 '17 at 18:34
  • 1
    I like this answer better than the answer in the linked question because it highlights that ConstraintLayout is superior to RelativeLayout and not just another flavor. – AnthonyW Apr 19 '17 at 00:42
  • It seems like you can use ConstraintLayout for pretty much everything without the need for anything else? – CodeMonkey Feb 19 '19 at 14:13