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:

To get started using ConstraintLayout , 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) :

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'

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 like RelativeLayouts “toLeftOf , toRightOf”, constraints offer similar options. They can also specify how widgets are aligned.

Types of Constraints:

Resize Handle Resize the widget by dragging its corners with the square icon.

Resize the widget by dragging its corners with the square icon. Size Constraint Handle – To specify the location of the widget, they are round circles on the side of each widget.

– To specify the location of the widget, they are round circles on the side of each widget. Baseline Constraint Handle – Allows you to align two baselines of text in your widgets.

– Allows you to align two baselines of text in your widgets. Bias– Specifies which direction you want your widget to have bias towards. You can specify vertical or horizontal bias.

Widget Sizing

In the side properties view, you will see an image depicting the constraints of the view size itself. Here you will be able to specify margins and decide how you want the the widget to resize, such as:

Match Constraint – Similar to match_parent except it obeys constraints – effectively 0dp.

– Similar to except it obeys constraints – effectively 0dp. Wrap Content – The widget will only be as big as it needs to be for all the content inside.

– The widget will only be as big as it needs to be for all the content inside. Fixed Size dp – Specifying a size of the widget in

To toggle between these, simply click on the current option in the properties window and it will toggle between all of them.

You will also be able to specify the bias by dragging the slider on the horizontal axis or vertical, depending on the bias you want (as shown above in the bias section).

Autoconnect

Autoconnect is a feature that automatically as you place a new widget in the layout, will start to calculate the best constraints to give a widget from where you have placed it.

As soon as you drag a widget into the layout, you will see automatic lines starting to draw from the sides of the widget, that is Autoconnect doing its job. This feature is enabled by default, and it can be toggled on and off, by clicking the magnet icon.

Manual Constraints

To create your own constraints, click a circle on the side of the widget you wish to position. Drag the line towards the other widget you want to align it to, once the widget turns green, it will create the constraint. You can delete a constraint by clicking on it when it is highlighted red around it.

Inference

Inference creates constraints between all different widgets in your layout, it acts like Autoconnect, but instead it works for the entire layout and not just the widget being edited. It uses complex mathematics to determine what widgets it should constrain to others, based on where you have placed it on the screen.

To use inference on a whole layout, click the sparkly yellow icon to see it work.

Other things to note:

Using this layout editor generates XML for you. If you still want to edit some XML you still can! But trust me, after using the Layout editor you will hardly be using the XML view.

But what about my old layouts? Should I migrate them all?

I wouldn’t jump on the train of changing everything at once if it’s not necessary. If you find your layouts are slow or you are battling to make changes then perhaps you should consider changing it to use ConstraintLayout .

An advertised feature of the new Android Studio is that you can automatically convert old layouts to new ones using the ConstraintLayout . When you navigate to your layout file and are in the design view, you can right click > “Convert …Layout to ConstraintLayout”.

My thoughts:

I am excited about this new layout editor and ConstraintLayout , I really think we needed something like this in Android. After playing around a bit it seems relatively simple to understand and quite powerful.

I hope that this could be turned into a standalone design tool that designers could ultimately use to create screens.

Links:

Layout Editor Code Lab

Android Studio Layout Editor Docs

Android Layouts – A new world – YouTube video from Google I/O