Since my initial blog post about ConstraintLayout, there have been a whole bunch of new features added (and lots of improvements) to ConstraintLayout. This blog post aims to cover some of the new features, namely Guidelines, Barriers, Chains and Groups.

Some of these features require using Android Studio 3.0 Beta 5 and the beta version of ConstraintLayout. In order to use the beta version of ConstraintLayout, make sure you have at least the following dependency in your app level build.gradle file:

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

Guidelines

Guidelines are small visual helpers to design layouts with. Views can be aligned to a guideline which can simplify layouts, especially if you have the same margin values duplicated on a lot of elements.

Guidelines can be specified in dp from start or end of the screen or they can be a percentage of the width of the screen. To cycle through the different guideline modes, you can click the round icon at the top of the guideline.

To create a guideline and use it, see below:

If you are curious as to how a guideline looks in XML, this is what it looks like:

<android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_begin="20dp" />

Other views in the layout can then constrain themselves to the guideline.

Barriers

Barriers are one of my favourite features in ConstraintLayout. A barrier is an invisible view that contains reference to the views that you wish to use to form a “barrier” against. If one of the views grows, the barrier will adjust its size to the largest height or width of the referenced items. Barriers can be vertical or horizontal and can be created to the top, bottom, left or right of the referenced views. Other views can then constrain themselves to the barrier.

Below is an example of creating a barrier from two views and constraining a view to it. As you can see, adjusting the size of the TextView, the barrier adjusts its size and the constrained view moves.

If you are curious as to how this is created, this is what it looks like in XML:

<android.support.constraint.Barrier android:id="@+id/barrier" android:layout_width="wrap_content" android:layout_height="wrap_content" app:barrierDirection="right" app:constraint_referenced_ids="button_example,text_view_status" />

The property app:constraint_referenced_ids contains a list of view ids that will form the barrier.

Chains

Chains allow you to control the space between elements and how the elements use the space. To create a chain, select the elements that you want to form part of the chain, and then right click – “Chain” – “Create Horizontal/Vertical Chain”.

You are then able to cycle through the different chain modes. There are four different modes: spread_inside, packed, spread and weighted.

The XML for creating a chain is different in that all the views have the constraints defined on them and the first item in the chain specifies the chainStyle.

<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintEnd_toStartOf="@+id/button_two" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" tools:text="1" />

Groups

With groups, you can logically group together certain views. Don’t confuse this with normal ViewGroups in Android though. A group in ConstraintLayout only contains references to the view ids and not nesting the views inside a group. With a group, you can set the visibility of all views in the group, by just setting the groups visibility without needing to set every view’s visibility. This is useful for things such as error screens or loading screens where a few elements need to change their visibility at once.

To add a group – see below:

If you are wondering how this looks in XML, here it is:

<android.support.constraint.Group android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" app:constraint_referenced_ids="button_load,text_view_status" />

The property app:constraint_referenced_ids contains a list of all the view ids that need to be a part of the group.

Practical Example

I wanted to try recreate the Google Play Movies detail screen only using ConstraintLayout to see how much I could build in the UI Editor. The results completely blew me away.

For the code relating to this layout, checkout this Github repository.

There are loads more features in ConstraintLayout, so subscribe to the blog to receive updates. Have anything to add? Let me know on Twitter.

Subscribe to Blog via Email

References: