Differences between ConstraintLayout and RelativeLayout

The question:

I am confused about the difference between ConstraintLayout and RelativeLayout. Could someone please tell me the exact differences between them?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

The Rules are similar to RelativeLayout, for example setting the bottom edge to the bottom of some other view.


Unlike RelativeLayout, ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

app:layout_constraintHorizontal_bias="0.33" <!-- from 0.0 to 1.0 -->
app:layout_constraintVertical_bias="0.53" <!-- from 0.0 to 1.0 -->

The Baseline handle (a long pipe with rounded corners, below the circle handle) is used to align the content of the view with another view reference.

Square handles (on each corner of the view) are used to resize the view in dps.

Differences between ConstraintLayout and RelativeLayout

This is totally opinion based and my impression of ConstraintLayout

Method 2

Relative Layout and Constraint Layout equivalent properties

Relative Layout and Constraint Layout equivalent properties

(1) Relative Layout:


(1) Constraint Layout equivalent :


(2) Relative Layout:


(2) Constraint Layout equivalent:


(3) Relative Layout:


(3) Constraint Layout equivalent:


(4) Relative Layout:


(4) Constraint Layout equivalent:


(5) Relative Layout:


(5) Constraint Layout equivalent:


(6) Relative Layout:


(6) Constraint Layout equivalent:


(7) Relative Layout:


(7) Constraint Layout equivalent:


(8) Relative Layout:


(8) Constraint Layout equivalent:


(9) Relative Layout:


(9) Constraint Layout equivalent:


(10) Relative Layout:


(10) Constraint Layout equivalent:


(11) Relative Layout:


(11) Constraint Layout equivalent:


(12) Relative Layout:


(12) Constraint Layout equivalent:


(13) Relative Layout:


(13) Constraint Layout equivalent:


(14) Relative Layout:


(14) Constraint Layout equivalent:


(15) Relative Layout:


(15) Constraint Layout equivalent:


(16) Relative Layout:


(16) Constraint Layout equivalent:


(17) Relative Layout:


(17) Constraint Layout equivalent:


(18) Relative Layout:


(18) Constraint Layout equivalent:


(19) Relative Layout:


(19) Constraint Layout equivalent:


(20) Relative Layout:


(20) Constraint Layout equivalent:


(21) Relative Layout:


(21) Constraint Layout equivalent:


(22) Relative Layout:


(22) Constraint Layout equivalent:


Method 3

Reported by @davidpbr ConstraintLayout performance

I made two similar 7-child layouts, one each with a parent ConstraintLayout and RelativeLayout.
Based on Android Studio method tracing tool, it appears the ConstraintLayout spends more time in onMeasure and performs additional work in onFinishInflate.

Library used (support-v4, appcompat-v7…):


Devices/Android versions reproduced on:
Samsung Galaxy S6 (SM-G920A. Sorry, no Nexus atm). Android 5.0.2

Quick method tracing comparison:


Sample Github repo: https://github.com/OnlyInAmerica/ConstraintLayoutPerf

Method 4

Following are the differences/advantages:

  1. Constraint Layout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout).

  2. A very powerful use is grouping of elements by forming a chain. This way we can form a group of views which as a whole can be placed in a desired way without adding another layer of hierarchy just to form another group of views.

  3. In addition to weights, we can apply horizontal and vertical bias which is nothing but the percentage of displacement from the centre. ( bias of 0.5 means centrally aligned. Any value less or more means corresponding movement in the respective direction ) .

  4. Another very important feature is that it respects and provides the functionality to handle the GONE views so that layouts do not break if some view is set to GONE through java code. More can be found here:

  5. Provides power of automatic constraint applying by the use of Blue print and Visual Editor tool which makes it easy to design a page.

All these features lead to flattening of the view hierarchy which improves performance and also helps in making responsive and dynamic UI which can more easily adapt to different screen size and density.

Here is the best place to learn quickly:

Method 5

A big difference is that ConstraintLayout respects constraints even if the view is gone. So it won’t break the layout if you have a chain and you want to make a view disappear in the middle.

Method 6

Officially, ConstraintLayout is much faster

In the N release of Android, the ConstraintLayout class provides similar functionality to RelativeLayout, but at a significantly lower cost.

Method 7

The Conclusion I can make is

1) We can do UI design without touching the xml part of code, to be honest I feel google has copied how UI is designed in iOS apps, it will make sense if you are familiar with UI development in iOS, but in relative layout its hard to set the constraints without touching the xml design.

2) Secondly it has flat view hierarchy unlike other layouts, so does better performance than relative layout which you might have seen from other answers

3) It also have extra things apart from what relative layout has, such as circular relative positioning where we can position another view relative to this one at certain radius with certain angle which cant do in relative layout

I am saying it again, designing UI using constraint layout is same as designing UI in iOS, so in future if you work on iOS you will find it easier if you have used constraint layout

Method 8

In addition to @dhaval-jivani answer.

I’ve updated the project github project to latest version of constraint layout v.1.1.0-beta3

I’ve measured and compared the time of onCreate method and time between a start of onCreate and end of execution of last preformDraw method which visible in CPU monitor. All test were done on Samsung S5 mini with android 6.0.1
Here results:

Fresh start (first screen opening after application launch)

Relative Layout

OnCreate: 123ms

Last preformDraw time – OnCreate time: 311.3ms

Constraint Layout

OnCreate: 120.3ms

Last preformDraw time – OnCreate time: 310ms

Besides that, I’ve checked performance test from this article , here the code
and found that on loop counts less than 100 constraint layout variant is faster during execution of inflating, measure, and layout then variants with Relative Layout. And on old Android devices, like Samsung S3 with Android 4.3, the difference is bigger.

As a conclusion I agree with comments from the article:

Does it worth to refactor old views switch on it from RelativeLayout or LinearLayout?

As always: It depends 🙂

I wouldn’t refactor anything unless you either have a performance problem with your current layout hierarchy or you want to make significant changes to the layout anyway. Though I haven’t measured it lately, I haven’t found any performance issues in the last releases. So I think you should be safe to use it. but – as I’v said – don’t just migrate for the sake of migrating. Only do so, if there’s a need for and benefit from it.
For new layouts, though, I nearly always use ConstraintLayout. It’s so much better compare to what we had before.

Method 9

The real question to ask is, is there any reason to use any layout other than a constraint layout? I believe the answer might be no.

To those insisting they are aimed at novice programmers or the like, they should provide some reason for them to be inferior to any other layout.

Constraints layouts are better in every way (They do cost like 150k in APK size.). They are faster, they are easier, they are more flexible, they react better to changes, they fix the problems when items go away, they conform better to radically different screen types and they don’t use a bunch of nested loop with that long drawn out tree structure for everything. You can put anything anywhere, with respect to anything, anywhere.

They were a bit screwy back in mid 2016, where the visual layout editor just wasn’t good enough, but they are to the point that if you are having a layout at all, you might want to seriously consider using a constraint layout, even when it does the same thing as a RelativeLayout, or even a simple LinearLayout. FrameLayouts clearly still have their purpose. But, I can’t see building anything else at this point. If they started with this they wouldn’t have added anything else.

Method 10

The only difference i’ve noted is that things set in a relative layout via drag and drop automatically have their dimensions relative to other elements inferred, so when you run the app what you see is what you get. However in the constraint layout even if you drag and drop an element in the design view, when you run the app things may be shifted around. This can easily be fixed by manually setting the constraints or, a more risky move being to right click the element in the component tree, selecting the constraint layout sub menu, then clicking ‘infer constraints’. Hope this helps

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment