In this tutorial, we’ll discuss the intricacies of android ConstraintLayout. Google had introduced android constraint layout editor at Google I/O Conference . The new Layout Editor has a set of powerful tools to allow developers to create flat-ui hierarchies for their complex layouts.

Android ConstraintLayout

To use android ConstraintLayout, make sure you’re using the latest Android Studio version. Ideally, Android Studio 2.2 and above. We need to download the necessary SDK Tools for ConstraintLayout from the SDK Manager.


Create a new empty activity project and add the following dependency inside the build.gradle file.

compile ''

Create a new layout with the root class set to ConstraintLayout as shown in the image below.

To convert an old layout into a ConstraintLayout. Open the design pane of the respective layout, right click the root component and choose the relevant option as shown in the image below.


Android Constraint Layout Overview

Android ConstraintLayout is used to define a layout by assigning constraints for every child view/widget relative to other views present.

A ConstraintLayout is similar to a RelativeLayout, but with more power. The aim of ConstraintLayout is to improve the performance of the applications by removing the nested views with a flat and flexible design.

A view inside the ConstraintLayout has handles(or anchor points) on each side which are used to assign the constraints. Let’s drag and drop a TextView on the layout and assign the constraints to it.

Android ConstraintLayout textview

The TextView above has three types of handles:

Resize handle

    1. – It’s present on the four corners and is used to resize the view, but keeping its constraints intact.

Side handle – It’s the circular handle present on the centre of each side. It’s used to set the top, left, bottom and right constraints of the view.

Baseline handle – It’s used to align the baseline with another textview in the layout.

Let’s assign the constraints on the TextView and look into the xml code of it.

Android ConstraintLayout example

Notice the Properties inspector pane at the right-hand side:

It shows the margins set for each side of the view. It also allows to change the size of the view by toggling between the following modes:

  • Wrap Content – This wraps the view to fill it’s content.
  • Any Size – This is similar to match parent.
  • Fixed Size – This allows us to set constant width and height.

The xml code for the above layout looks like:


    • app:layout_constraintLeft_toLeftOf="parent" – constraintLeft is the anchor point to the left of the view. toLeftOf signifies aligning the view to the left of the assigned view which is “parent” in this case.

When the absolute positions are set on the view, the xml properties that are used are –

Let’s add another TextView and align their baselines.

Android ConstraintLayout tutorial

The xml code for the above layout is :

There are two other tools Auto-connect and Inferences that are used to created constraints automatically.

  1. Auto-Connect – This feature can be enabled by clicking:Android ConstraintLayout example
    As the name suggests, Auto-connect creates constraints automatically between two neighbouring views
  2. Inference – This feature is enabled by clicking:Android ConstraintLayout example tutorial
    The Inference engine creates constraints among all elements in a layout. The inference engine tries to find and create optimal connections based on various factors such as the positions of the widgets and their size.

A demo of Auto-Connect layout is given below:
Android ConstraintLayout auto connect

As you can see in the above gif, The constraints are animated automatically.
Notice the horizontal and vertical sliders in the properties pane. They are called horizontal and vertical bias respectively. They allow us to position a view along the horizontal or vertical axis using a bias value, this will be relative to it’s constrained position.

The xml code for the above demo is given below:

Demo of a layout with Inference enabled is given below:
Android ConstraintLayout inferences

The xml code for the above gif is :

tools:layout_constraintTop_creator="1": The creator attributes to keep track of who created the constraints, particularly if they are created by the inference engine they’re assigned as 1.

Deleting Constraints

To delete an individual constraint, hover over the circular handle and click it has it turns red.
To delete all constraints of a view, click the symbol underneath it that looks like:
Android ConstraintLayout delete

A sample demo gif is given below:
Android Constraint Layout delete

This brings an end to this tutorial. You can go ahead and try replacing some of your own layouts with a constraint layout.

By admin

Leave a Reply

%d bloggers like this: