{"id":2554,"date":"2018-07-17T05:09:54","date_gmt":"2018-07-17T05:09:54","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=2554"},"modified":"2018-07-17T05:09:54","modified_gmt":"2018-07-17T05:09:54","slug":"constraintlayout","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/constraintlayout","title":{"rendered":"Constraint Layout Tutorial With Example In Android Studio [Step by Step]"},"content":{"rendered":"<p>Constraint Layout is a ViewGroup (i.e. a view that holds other views) which allows you to create large and complex layouts with a flat view hierarchy, and also allows you to position and size widgets in a very flexible way. It was created to help reduce the nesting of views and also improve the performance of layout files.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2745\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/07\/Constraint-Layout-Example-In-Android-Studio-1.png\" alt=\"Constraint Layout Example In Android Studio\" width=\"450\" height=\"377\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/07\/Constraint-Layout-Example-In-Android-Studio-1.png 450w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/07\/Constraint-Layout-Example-In-Android-Studio-1-300x251.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>ConstraintLayout is very similar to RelativeLayout in such a way because, views are laid out according to relationships between sibling views and the parent layout yet it\u2019s a lot more flexible and works better with the Layout Editor of the Android Studio\u2019s. It was released at Google I\/O 2016. Since it came into existence (i.e. as at Android studio 2.3), it has become a wildly used viewgroup and supports Android 2.3 or higher.<\/p>\n<hr \/>\n<h4><strong>Advantages Of Constraint Layout Over Other Layouts<\/strong><\/h4>\n<p>1. One great advantage of the constraintlayout is that you can perform animations on your ConstraintLayout views with very little code.<br \/>\n2. You can build your complete layout with simple drag-and-drop on the Android Studio design editor.<br \/>\n3. You can control what happens to a group of widgets through a single line of code.<br \/>\n4. Constraint Layout improve performance over other layout<\/p>\n<hr \/>\n<h4><strong>Using Constraint Layout In Android Studio:<\/strong><\/h4>\n<p>It is not bundled as part of Android SDK and is available as a support library. Due to this, any update in the future would be compatible with all versions of Android.<\/p>\n<p>To use Constraint Layout make sure you have declared below repository in build.gradle file<\/p>\n<pre>repositories {\r\n    maven {\r\n        url 'https:\/\/maven.google.com'\r\n    }\r\n}<\/pre>\n<p>Now to use ConstraintLayout features in our android project, we will need to add the library to our build.gradle app module dependencies section.<br \/>\nOpen your build.gradle (Module app) and add the code below:<\/p>\n<pre>dependencies {\r\n    compile 'com.android.support.constraint:constraint-layout:1.1.0-beta3'\r\n}\r\n<\/pre>\n<hr \/>\n<h4><strong>Design Or Blueprint Mode In Android Studio:<\/strong><\/h4>\n<p>In Android Studio design and blueprint mode are added which display the layout design in design and blueprint mode. You can enable any mode or both together according to your requirement.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> To help you understand ConstraintLayout, we will enable both(design and blueprint mode) for this tutorial.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2706\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/design-and-blueprint-mode-android-studio.jpg\" alt=\"Design and blueprint mode android studio\" width=\"745\" height=\"512\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/design-and-blueprint-mode-android-studio.jpg 745w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/design-and-blueprint-mode-android-studio-300x206.jpg 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/p>\n<hr \/>\n<h4><strong>Understanding Handles or Anchor Points In Constraint Layout:<\/strong><\/h4>\n<p>Lets suppose you drag a TextView element in ConstraintLayout visual editor of Android Studio. Immediately after dragging you will notice a error with a message, &#8220;This view is not constrained&#8230;&#8221; So this simply means the view we created is not Constrained and we need to fix it. If we don&#8217;t fix it, the view won&#8217;t render properly when it will run in App.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2710\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/View-not-constrained-android-studio-constraintlayout.png\" alt=\"View not constrained android studio ConstraintLayout\" width=\"618\" height=\"438\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/View-not-constrained-android-studio-constraintlayout.png 618w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/View-not-constrained-android-studio-constraintlayout-300x213.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/p>\n<p>Now I hover around the button, you can see different points which can be called as <strong>handles or anchor points<\/strong> in Constraint Layout.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2711\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/handle-anchor-point-constraintlayout-android-studio.png\" alt=\"handle anchor point Constraint Layout Android Studio\" width=\"275\" height=\"224\" \/><\/p>\n<p>Click on the any handle and drag it to make connection with something else around it.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> You will need to make at least two connection of handles with something else to make it Constrained. So this way you can create Constrained.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2712\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Handle-ConstraintLayout-Android-Studio.gif\" alt=\"Handle ConstraintLayout Android Studio\" width=\"444\" height=\"156\" \/><\/p>\n<p><strong>Resize handle \u2013<\/strong> To resize the view size, you can use resize handle found at the corners which keeps constraint intact. Just simply drag and resize it according to your App UI requirements.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2713\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Resize-Handle-ConstraintLayout.gif\" alt=\"Resize Handle ConstraintLayout\" width=\"430\" height=\"191\" \/><\/p>\n<p><strong>Side handle \u2013<\/strong> The side handle are circular handle used to set the top, left, bottom and right constraints of the view.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2714\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Side-handle-ConstraintLayout.png\" alt=\"Side handle ConstraintLayout\" width=\"124\" height=\"89\" \/><\/p>\n<hr \/>\n<h4><strong>Attribute Window For ConstraintLayout:<\/strong><\/h4>\n<p>On the right side you will notice a attribute window which share lots of details about the views that we used for View in ConstraintLayout.<\/p>\n<p>You control sizing of the element by clicking on 4 side arrows to change wrap_content to match_constrained, fixed size etc.<\/p>\n<p><strong>Bias<\/strong> decides\u00a0view placement between its constraints on an axis. By default it is set 50% and can be changed easily by dragging.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Biasing is difficult to achieve in Linear Layout, Relative layout etc.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2723\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/window-attribute-constraintlayout-android-studio.png\" alt=\"Window attribute constraintlayout android studio\" width=\"473\" height=\"387\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/window-attribute-constraintlayout-android-studio.png 473w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/window-attribute-constraintlayout-android-studio-300x245.png 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/p>\n<h4><strong>Deleting Constraint In ConstraintLayout:<\/strong><\/h4>\n<p>To delete the constrained connection, simply click on handle point and thats it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2725\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Delete-Constrained-Constraint-Layout-Android-Studio.gif\" alt=\"Delete Constrained Constraint Layout Android Studio\" width=\"304\" height=\"276\" \/><\/p>\n<p><b>Different Tools In ConstraintLayout:<\/b><\/p>\n<p>You can also use tools like Autoconnect to let Android Studio make automatic connection of view, clear all constraints to delete all constraints in one go and infer constraint to automatic figure our the constraints for all the views on screen.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2726\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Tools-In-ConstraintLayout-Android-Studio.png\" alt=\"Tools-In-ConstraintLayout-Android-Studio\" width=\"316\" height=\"161\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Tools-In-ConstraintLayout-Android-Studio.png 316w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Tools-In-ConstraintLayout-Android-Studio-300x153.png 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/p>\n<hr \/>\n<h4><strong>Relative Positioning In Constraint Layout:<\/strong><\/h4>\n<p>Relative Positioning is the most important type of Constraint Layout and considered as the basic block building in it. The different constraint option it offers works in relation\/relative to position of one another. Those relative positioning works only in vertical and horizontal axis only.<\/p>\n<p>Using horizontal axis, you can set positioning of one widget in right, left, end and start sides of other widget. While using vertical axis you can set bottom, top sides and text baseline.<\/p>\n<p>Let\u2019s now understand the concept by taking an example. In the below example, we will create two TextView. In first TextView we will write Hello and in second we will write AbhiAndroid. We will set position of \u201cAbhiAndroid\u201d left of the \u201cHello\u201d but in the right side. Below is the XML code:<\/p>\n<p>Here constraint means that the system will try to share the same location with both sides. The textview1 is left side of textview2 to be constrained to the right side of textview1.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/textview1\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Hello \"\/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/textview2\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        app:layout_constraintLeft_toRightOf=\"@id\/textview1\"\r\n        android:text=\"AbhiAndroid\"\/&gt;\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> To define a view&#8217;s position in ConstraintLayout, you must add at least one horizontal and one vertical constraint to the view. Each constraint defines the view&#8217;s position along either the vertical or horizontal axis; so each view must have a minimum of one constraint for each axis, but often more are necessary. There are several types of restrictions. In particular, the following are some of the restrictions that can be used to set a position relative to another item:<\/p>\n<p><strong>layout_constraintLeft_toLeftOf :<\/strong> the left border of the element is positioned relative to the left border of another element<\/p>\n<p><strong>layout_constraintLeft_toRightOf :<\/strong> the left border of the element is positioned relative to the right border of another element<\/p>\n<p><strong>layout_constraintRight_toLeftOf:<\/strong> the right border of the element is positioned relative to the left border of another element<\/p>\n<p><strong>layout_constraintRight_toRightOf:<\/strong> the right border of the element is positioned relative to the right border of another element.<\/p>\n<h4><strong>Chains In ConstraintLayout:<\/strong><\/h4>\n<p>Chains allow us to control the space between elements and chains all the selected elements to another.<\/p>\n<p><strong>To create a chain, select the elements that you want to form part of the chain, and then right click \u2013 \u201cChain\u201d \u2013 \u201cCreate Horizontal or Vertical Chain\u201d.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2730\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Chains-In-Constraint-Layout-Android-Studio.gif\" alt=\"Chains In Constraint Layout Android Studio\" width=\"301\" height=\"248\" \/><\/p>\n<p>The different available chain style are spread, spread_inside and packed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2731\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/chain-style-constraint-layout-android-studio.png\" alt=\"Chain style constraint layout android studio\" width=\"423\" height=\"330\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/chain-style-constraint-layout-android-studio.png 423w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/chain-style-constraint-layout-android-studio-300x234.png 300w\" sizes=\"auto, (max-width: 423px) 100vw, 423px\" \/><\/p>\n<p>You can do both\u00a0Horizontal or Vertical Chain at the same time.<\/p>\n<p>The XML for creating a chain is different in that all the views have the constraints defined on them and the <strong>first item<\/strong> in the chain specifies the chainStyle.<\/p>\n<p>Below is the example XML code of using chains in Constraint Layout<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"\r\n    tools:layout_editor_absoluteY=\"81dp\"&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/button5\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginBottom=\"8dp\"\r\n        android:layout_marginTop=\"8dp\"\r\n        android:text=\"Button\"\r\n        app:layout_constraintBottom_toBottomOf=\"parent\"\r\n        app:layout_constraintEnd_toStartOf=\"@+id\/button6\"\r\n        app:layout_constraintHorizontal_bias=\"0.5\"\r\n        app:layout_constraintHorizontal_chainStyle=\"spread\"\r\n        app:layout_constraintStart_toStartOf=\"parent\"\r\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/button6\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginBottom=\"8dp\"\r\n        android:layout_marginTop=\"8dp\"\r\n        android:text=\"Button\"\r\n        app:layout_constraintBottom_toBottomOf=\"parent\"\r\n        app:layout_constraintEnd_toEndOf=\"parent\"\r\n        app:layout_constraintHorizontal_bias=\"0.5\"\r\n        app:layout_constraintStart_toEndOf=\"@+id\/button5\"\r\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/pre>\n<h4><strong>Using Guidelines For Designing Layout In ConstraintLayout<\/strong><\/h4>\n<p>You can use guidelines to define any vertical or horizontal guideline while designing your App Layout. This will help you make Constrained connection of view to guideline and design layout keeping guideline in mind.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2733\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Guidelines-Constraint-Layout-Android-Studio.gif\" alt=\"Guidelines Constraint Layout Android Studio\" width=\"546\" height=\"264\" \/><\/p>\n<p><strong>Important Note:<\/strong> On changing the margin or percentage of guideline, it will also change automatically for all connected views also.<\/p>\n<hr \/>\n<h4><strong>Percent dimensions In ConstraintLayout:<\/strong><\/h4>\n<p>The <strong>percent<\/strong> value of the constraint Width_default, allow us to set a widget to take some percentage of the available space.<\/p>\n<pre>&lt;!-- the widget will take 80% of the available space --&gt;\r\n\r\napp:layout_constraintWidth_default=\"percent\"\r\napp:layout_constraintWidth_percent=\"0.8\"<\/pre>\n<h4><strong>Barriers In ConstraintLayout:<\/strong><\/h4>\n<p>The <strong>Barriers<\/strong> usually avoid one or more widgets\/elements to bypass it. When a widget tries to pass through it, the Barrier will move itself, and avoiding the widget to be placed above it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2735\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Barriers-in-ConstraintLayout-Android-Studio.gif\" alt=\"Barriers in ConstraintLayout Android Studio\" width=\"588\" height=\"282\" \/><\/p>\n<p>Below is the example XML code of using Barriers in ConstraintLayout<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"\r\n    tools:layout_editor_absoluteY=\"81dp\"&gt;\r\n\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/button13\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginStart=\"8dp\"\r\n        android:layout_marginTop=\"8dp\"\r\n        android:text=\"Button\"\r\n        app:layout_constraintStart_toStartOf=\"parent\"\r\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/textView8\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginStart=\"8dp\"\r\n        android:layout_marginTop=\"8dp\"\r\n        android:text=\"Barriers avoid overlapping of elements\"\r\n        app:layout_constraintStart_toStartOf=\"parent\"\r\n        app:layout_constraintTop_toBottomOf=\"@+id\/button13\" \/&gt;\r\n\r\n    &lt;android.support.constraint.Barrier\r\n        android:id=\"@+id\/barrier8\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        app:barrierDirection=\"right\"\r\n        app:constraint_referenced_ids=\"button13,textView8\"\r\n        tools:layout_editor_absoluteY=\"511dp\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/button14\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginStart=\"8dp\"\r\n        android:text=\"Button\"\r\n        app:layout_constraintStart_toEndOf=\"@+id\/barrier8\"\r\n        tools:layout_editor_absoluteY=\"34dp\" \/&gt;\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/pre>\n<hr \/>\n<h4><strong>Group In ConstraintLayout:<\/strong><\/h4>\n<p>Group in android helps to carry out some actions on a set of widgets with the most common case being to control the visibility of a collection of widgets.<\/p>\n<p>When faced with this scenario, the most common solution was to maintain yourself a list or set of views inside the Activity or Fragment, or even adding a ViewGroup and put all the views inside of it, then control the visibility of the container. Now to carry out the action on the views, you only need to add their ids as the referenced ids in the group, and group in the ConstraintLayout will propagate the actions to all plugged views.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2737\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/06\/Group-In-Constraint-Layout-Android-Studio.gif\" alt=\"Group In Constraint Layout Android Studio\" width=\"600\" height=\"215\" \/><\/p>\n<p>In the below XML example code of using Groups in ConstraintLayout, we have set visibility to invisible of two Button:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"\r\n    tools:layout_editor_absoluteY=\"81dp\"&gt;\r\n\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/button15\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button\"\r\n        tools:layout_editor_absoluteX=\"44dp\"\r\n        tools:layout_editor_absoluteY=\"28dp\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/button16\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Button\"\r\n        tools:layout_editor_absoluteX=\"89dp\"\r\n        tools:layout_editor_absoluteY=\"118dp\" \/&gt;\r\n\r\n    &lt;android.support.constraint.Group\r\n        android:id=\"@+id\/group\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:visibility=\"invisible\"\r\n        app:constraint_referenced_ids=\"button16,button15\" \/&gt;\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Constraint Layout Example in Android Studio:<\/strong><\/h4>\n<p>Below we design the simple Login screen in Constraint Layout. We used ImageView, EditText, Button and TextView for designing the below layout.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2743\" src=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/07\/Constraint-Layout-Example-In-Android-Studio.png\" alt=\"Constraint-Layout-Example-In-Android-Studio\" width=\"450\" height=\"377\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/07\/Constraint-Layout-Example-In-Android-Studio.png 450w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2018\/07\/Constraint-Layout-Example-In-Android-Studio-300x251.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p><strong>Step 1:<\/strong>\u00a0Create a new project\u00a0ConstraintLayout and activity Main Activity. Here we will create a Constraint Layout:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/constraintLayout\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:background=\"#80C938\"&gt;\r\n\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/pre>\n<p><strong>Step 2:<\/strong> Add other views(imageview, edittext, button and textview) by adding both horizontal and vertical constraints to them:<\/p>\n<pre>&lt;ImageView\r\nandroid:layout_width=\"146dp\"\r\nandroid:layout_height=\"100dp\"\r\nandroid:src=\"@drawable\/abhi_android\"\r\nandroid:id=\"@+id\/imageView\"\r\nandroid:layout_marginTop=\"96dp\"\r\napp:layout_constraintTop_toTopOf=\"parent\"\r\nandroid:layout_marginEnd=\"16dp\"\r\napp:layout_constraintRight_toRightOf=\"parent\"\r\nandroid:layout_marginRight=\"16dp\"\r\nandroid:layout_marginStart=\"16dp\"\r\napp:layout_constraintLeft_toLeftOf=\"parent\"\r\nandroid:layout_marginLeft=\"16dp\" \/&gt;\r\n\r\n&lt;EditText\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:inputType=\"textPersonName\"\r\nandroid:ems=\"10\"\r\nandroid:id=\"@+id\/editText\"\r\nandroid:layout_marginStart=\"16dp\"\r\nandroid:padding=\"12dp\"\r\napp:layout_constraintLeft_toLeftOf=\"parent\"\r\nandroid:layout_marginLeft=\"16dp\"\r\nandroid:layout_marginEnd=\"16dp\"\r\napp:layout_constraintRight_toRightOf=\"parent\"\r\nandroid:layout_marginRight=\"16dp\"\r\nandroid:hint=\"Email\"\r\nandroid:background=\"#ffffff\"\r\nandroid:layout_marginTop=\"232dp\"\r\napp:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n&lt;EditText\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:inputType=\"textPassword\"\r\nandroid:ems=\"10\"\r\nandroid:id=\"@+id\/editText2\"\r\nandroid:padding=\"12dp\"\r\nandroid:background=\"#ffffff\"\r\nandroid:layout_marginEnd=\"16dp\"\r\napp:layout_constraintRight_toRightOf=\"parent\"\r\nandroid:layout_marginRight=\"16dp\"\r\nandroid:layout_marginStart=\"16dp\"\r\napp:layout_constraintLeft_toLeftOf=\"parent\"\r\nandroid:layout_marginLeft=\"16dp\"\r\nandroid:hint=\"Password\"\r\nandroid:layout_marginTop=\"304dp\"\r\napp:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:text=\"SIGN IN\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#00773F\"\r\nandroid:id=\"@+id\/button1\"\r\nandroid:layout_marginStart=\"16dp\"\r\nandroid:paddingLeft=\"30dp\"\r\nandroid:paddingRight=\"30dp\"\r\nandroid:textColor=\"#ffffff\"\r\napp:layout_constraintLeft_toLeftOf=\"parent\"\r\nandroid:layout_marginLeft=\"16dp\"\r\nandroid:layout_marginEnd=\"16dp\"\r\napp:layout_constraintRight_toRightOf=\"parent\"\r\nandroid:layout_marginRight=\"16dp\"\r\nandroid:layout_marginTop=\"408dp\"\r\napp:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n&lt;TextView\r\nandroid:text=\"Don't have an account?\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:id=\"@+id\/textView\"\r\nandroid:layout_marginEnd=\"16dp\"\r\napp:layout_constraintRight_toRightOf=\"parent\"\r\nandroid:layout_marginRight=\"16dp\"\r\nandroid:layout_marginStart=\"16dp\"\r\nandroid:textColor=\"#aaffffff\"\r\napp:layout_constraintLeft_toLeftOf=\"parent\"\r\nandroid:layout_marginLeft=\"16dp\"\r\nandroid:layout_marginTop=\"480dp\"\r\napp:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n<\/pre>\n<p><strong>Step 3:<\/strong> The complete xml code of simple Constraint Layout example:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/constraintLayout\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:background=\"#80C938\"&gt;\r\n\r\n&lt;ImageView\r\n        android:layout_width=\"146dp\"\r\n        android:layout_height=\"100dp\"\r\n        android:src=\"@drawable\/abhi_android\"\r\n        android:id=\"@+id\/imageView\"\r\n        android:layout_marginTop=\"96dp\"\r\n        app:layout_constraintTop_toTopOf=\"parent\"\r\n        android:layout_marginEnd=\"16dp\"\r\n        app:layout_constraintRight_toRightOf=\"parent\"\r\n        android:layout_marginRight=\"16dp\"\r\n        android:layout_marginStart=\"16dp\"\r\n        app:layout_constraintLeft_toLeftOf=\"parent\"\r\n        android:layout_marginLeft=\"16dp\" \/&gt;\r\n\r\n    &lt;EditText\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:inputType=\"textPersonName\"\r\n        android:ems=\"10\"\r\n        android:id=\"@+id\/editText\"\r\n        android:layout_marginStart=\"16dp\"\r\n        android:padding=\"12dp\"\r\n        app:layout_constraintLeft_toLeftOf=\"parent\"\r\n        android:layout_marginLeft=\"16dp\"\r\n        android:layout_marginEnd=\"16dp\"\r\n        app:layout_constraintRight_toRightOf=\"parent\"\r\n        android:layout_marginRight=\"16dp\"\r\n        android:hint=\"Email\"\r\n        android:background=\"#ffffff\"\r\n        android:layout_marginTop=\"232dp\"\r\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n    &lt;EditText\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:inputType=\"textPassword\"\r\n        android:ems=\"10\"\r\n        android:id=\"@+id\/editText2\"\r\n        android:padding=\"12dp\"\r\n        android:background=\"#ffffff\"\r\n        android:layout_marginEnd=\"16dp\"\r\n        app:layout_constraintRight_toRightOf=\"parent\"\r\n        android:layout_marginRight=\"16dp\"\r\n        android:layout_marginStart=\"16dp\"\r\n        app:layout_constraintLeft_toLeftOf=\"parent\"\r\n        android:layout_marginLeft=\"16dp\"\r\n        android:hint=\"Password\"\r\n        android:layout_marginTop=\"304dp\"\r\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:text=\"SIGN IN\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:background=\"#00773F\"\r\n        android:id=\"@+id\/button1\"\r\n        android:layout_marginStart=\"16dp\"\r\n        android:paddingLeft=\"30dp\"\r\n        android:paddingRight=\"30dp\"\r\n        android:textColor=\"#ffffff\"\r\n        app:layout_constraintLeft_toLeftOf=\"parent\"\r\n        android:layout_marginLeft=\"16dp\"\r\n        android:layout_marginEnd=\"16dp\"\r\n        app:layout_constraintRight_toRightOf=\"parent\"\r\n        android:layout_marginRight=\"16dp\"\r\n        android:layout_marginTop=\"408dp\"\r\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:text=\"Don't have an account?\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_marginEnd=\"16dp\"\r\n        app:layout_constraintRight_toRightOf=\"parent\"\r\n        android:layout_marginRight=\"16dp\"\r\n        android:layout_marginStart=\"16dp\"\r\n        android:textColor=\"#aaffffff\"\r\n        app:layout_constraintLeft_toLeftOf=\"parent\"\r\n        android:layout_marginLeft=\"16dp\"\r\n        android:layout_marginTop=\"480dp\"\r\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\r\n\r\n\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now look at the layout in design mode to see the Layout of simple Login screen created using Constraint Layout.<\/p>\n<hr \/>\n<h4><strong>Important Points About Constraint Layout:<\/strong><\/h4>\n<p>To use ConstraintLayout,<br \/>\ni. Your Android Studio version must be 2.3 or higher.<br \/>\nii. You must first include the ConstraintLayout&#8217;s support library.<br \/>\niii. You must have at least one vertical and one horizontal constraints.<\/p>\n<p>In conclusion, ConstraintLayout is a faster, better and much more efficient choice to designing large and aesthetic layouts in your Android UI.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Constraint Layout is a ViewGroup (i.e. a view that holds other views) which allows you to create large and complex layouts with a flat view hierarchy, and also allows you to position and size widgets in a very flexible way. It was created to help reduce the nesting of views and also improve the performance &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/constraintlayout\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Constraint Layout Tutorial With Example In Android Studio [Step by Step]<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-2554","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ConstraintLayout Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Follow complete ConstraintLayout tutorial step by step and learn how to use A ConstraintLayout with example in Android Studio. it is a ViewGroup which allows you to create large and complex layouts with a flat view hierarchy, and also allows you to position and size widgets in a very flexible way.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/constraintlayout\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=2554"}],"version-history":[{"count":38,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2554\/revisions"}],"predecessor-version":[{"id":2746,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2554\/revisions\/2746"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}