ConstraintLayout Android Studio 2.2
- 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.
What is ConstraintLayout?
ConstraintLayout is a new type of layout that you can use in your Android App, it is compatible down to API level 9 and is part of the support library. Its aim is to reduce layout hierarchies (and improve performance of layouts) and it also reduces the complexity of trying to work with RelativeLayouts . It is compatible with other layouts, so they can live in harmony together without having to choose one or the other.
Getting Started with ConstraintLayout:
You need to download Android Studio 2.2 Preview 3 to try it out.
To get started using ConstraintLayout , you can add the following to your build.gradle dependencies (or Android Studio will automatically add the ConstraintLayout dependency to your build.gradle when you create a new layout using it)
To use the new ConstraintLayout , click on res/layout folder. Select New > Layout Resource Folder. In the root element option, remove LinearLayout and start typing ConstraintLayout. . You will then create a new file with your ConstraintLayout , the default view includes the Blueprint mode
and Design Mode
which will look something like this:


How does this new layout builder work?
Constraints:
Constraints are rules that you define for a widget, much likeRelativeLayouts “toLeftOf , toRightOf”, constraints offer similar options. They can also specify how widgets are aligned.
Definition
ConstraintLayout is a new type of layout introduced by Android, similar to RelativeLayout but more flexible, and it is used to position UI elements relative to other UI elements by using constraints.
A constraint represent the rules we have to apply in order to position our UI elements relative to each other.
Overview
- ConstraintLayout has backwards compatibility (it is available in Support Library compatible with Android 2.3 and higher)
- When you create a new project, ConstraintLayout is used now in the template instead of RelativeLayout
- ConstraintLayout can be used directly from xml, but it is recommended to use the new Layout Editor
- Layout editor is available from Android Studio 2.2 Preview and was build especially for this new type of layout
- constraints can be created:
- manually
- by using Autoconnect (before, the layout editor started with Autoconnect enabled, but from Android Studio 2.2 Preview 5, it is now disabled by default. Also, the icon will be visible only if you use ConstraintLayout).
- by using Inference
- the difference between Autoconnect and Inference is that, Autoconnect creates constraints for the widget being currently moved (the current widget is constrained by other widgets, but other widgets won’t be constrained by the widget currently being moved) , whereas Inference creates constraints among all elements in a layout.
- constraints can be defined for one or more sides of a view by connecting the view to:
- an anchor point on another view
- an edge of the layout
- an invisible guideline
Rules for the Constraint System
- anchors on different axis cannot be connected (such as top and left anchor)
- baseline constraint can only be constrained to another baseline
- constraints resulting in a cycle are not permitted
References