ConstraintLayout 101 & the new Layout Builder in Android Studio

I was lucky enough to attend Google I/O and I went to the talk about the new Layout – ConstraintLayout . I was really excited about this feature that I couldn’t wait till I got home to try it out.

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.  With this new layout comes a new layout editor (which I am told was designed from the ground up, *huge sigh of relief* as the old one was really unusable).

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 Blueprint Mode and Design Mode Design Mode  which will look something like this:

ConstraintLayout

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.
[adwords_square]

Types of Constraints:

  • Resize Handle Resize Handle -Constraint Layout Resize the widget by dragging its corners with the square icon. Resize Handle - Constraint Layout
  • Size Constraint Handle Size Constraint Handle– To specify the location of the widget, they are round circles on the side of each widget.  Constraints
  • Baseline Constraint Handle – Allows you to align two baselines of text in your widgets. Baseline ConstraintLayout
  • Bias– Specifies which direction you want your widget to have bias towards. You can specify vertical or horizontal bias.Bias - ConstaintLayout

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:

  • Any Size ConstraintLayout - Any Size – Similar to match_parent  except it obeys constraints – effectively 0dp.
  • Wrap Content Wrap Content - ConstraintLayout– The widget will only be as big as it needs to be for all the content inside.
  • Fixed Size  Fixed Size - Constraint Layout – Specifying a size of the widget in dp 

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

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. Autoconnect ConstraintLayout

autoconnect - constraintlayout

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.

Manual constraints - ConstraintLayout

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 crazy 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 lightbulb icon to see it work. Inference - ConstraintLayout

inference - ConstraintLayout

Other things to note:

  • The new layout editor does not depend on you using a ConstraintLayout , it works for older layouts too – some of the features like automatic inference won’t work.
  • Using this layout editor generates XML for you, so if you want to be hardcore and write the XML yourself, you are welcome to.
  • You can use an array of other layout properties, such as:
    • Setting default margin
    • Align a group of widgets right, centre, equal distribution just like a design program would offer you.

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 . It is still in early alpha.

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”.

I am not sure if I was doing something wrong but for all three times I tried, the ConstraintLayout  was added to the view but results were not as expected. I suspect this feature is still going to take a while before it works for every use case –  it is still in alpha after all 😊.

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.

What are your thoughts? Have you tried out ConstraintLayout?
[adwords_square]
Links:

https://codelabs.developers.google.com/codelabs/constraint-layout/index.html

http://tools.android.com/tech-docs/layout-editor

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


If you enjoyed this blog post – please consider buying me a cupcake!
[buy_cupcake]

20 thoughts on “ConstraintLayout 101 & the new Layout Builder in Android Studio”

  1. Nice introduction to constraint layouts! Thanks! Which tool did you use to create these animated gifs?

  2. Thanks Rebecca! Constraint layouts is something very interesting for me, I want to experiment with them and your article is really helpful giving a basic intro to them!

  3. This may be interesting for performance, copied from Reddit:

    “I talked to the dude who made the layout at IO – the idea is that one constraint layout can replace multiple nestings of linear/relative layouts and doesn’t require the double pass that relative layout requires, so ideally it should be faster.”

  4. Great introduction. I always prefer tutorials that is dead simple to understand unlike android developer site .I found yours 🙂

  5. How is this layout better performance wise then a RelativeLayouts. if it is just for the tool i don’t see the benefit

  6. looks like xcode feature stolen my android studio …. but this will make android layout designs hell lot easier … excited to use this new layout

  7. Relative Layouts use deeper layouts and hence are complex to render and ultimately takes time, but with Constraint Layouts you don’t need to have deeper layouts , thus eliminating complex layouts . Ultimately its faster than Relative Layouts

  8. It is in its early stages and the team is still working on improving the performance of ConstraintLayout.
    With regards to measuring, it will do only one pass per widget, unless the widget is constrained to “match constraints” (0dp) – in that case it will do two measures for that widget.

    I find it can also do a lot more than RelativeLayout. The ability to set bias and guidelines would be quite difficult to achieve with RelativeLayout.

  9. Great Introduction to ConstraintLayout, thank you.
    I’m still wondering what it means in terms of layout to connect for example left side of one layout with the bottom of another layout. Left-to-right, top-bottom etc. connections are understandable but right-top or left-bottom are kinda confusing me.

  10. New > Layout Resource File? There is no New > Layout Resource Folder. There is New > Directory however. Walls.

Comments are closed.