Skip to content

Most visited

Recently visited



public class ConstraintLayout
extends ViewGroup

   ↳ android.view.View
     ↳ android.view.ViewGroup

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way.

Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning in enriching its API and capabilities over time. This documentation will reflects the changes.

There are currently various types of constraints that you can use:

Note that you cannot have a circular dependency in constraints.

Also see ConstraintLayout.LayoutParams for layout attributes

Developer Guide

Relative positioning

Relative positioning is one of the basic building block of creating layouts in ConstraintLayout. Those constraints allow you to position a given widget relative to another one. You can constrain a widget on the horizontal and vertical axis:

  • Horizontal Axis: Left, Right, Start and End sides
  • Vertical Axis: top, bottom sides and text baseline

The general concept is to constrain a given side of a widget to another side of any other widget.

For example, in order to position button B to the right of button A (Fig. 1):

Fig. 1 - Relative Positioning Example

you would need to do:

<Button android:id="@+id/buttonA" ... />
         <Button android:id="@+id/buttonB" ...
                 app:layout_constraintLeft_toRightOf="@+id/buttonA" />
This tells the system that we want the left side of button B to be constrained to the right side of button A. Such a position constraint means that the system will try to have both sides share the same location.

Fig. 2 - Relative Positioning Constraints

Here is the list of available constraints (Fig. 2):

  • layout_constraintLeft_toLeftOf
  • layout_constraintLeft_toRightOf
  • layout_constraintRight_toLeftOf
  • layout_constraintRight_toRightOf
  • layout_constraintTop_toTopOf
  • layout_constraintTop_toBottomOf
  • layout_constraintBottom_toTopOf
  • layout_constraintBottom_toBottomOf
  • layout_constraintBaseline_toBaselineOf
  • layout_constraintStart_toEndOf
  • layout_constraintStart_toStartOf
  • layout_constraintEnd_toStartOf
  • layout_constraintEnd_toEndOf

They all takes a reference id to another widget, or the parent (which will reference the parent container, i.e. the ConstraintLayout):

<Button android:id="@+id/buttonB" ...
                 app:layout_constraintLeft_toLeftOf="parent" />


Fig. 3 - Relative Positioning Margins

If side margins are set, they will be applied to the corresponding constraints (if they exist) (Fig. 3), enforcing the margin as a space between the target and the source side. The usual layout margins attributes can be used to this effect:

  • android:layout_marginStart
  • android:layout_marginEnd
  • android:layout_marginLeft
  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom

Note that a margin can only be positive or equals to zero, and takes a Dimension.

Margins when connected to a GONE widget

When a position constraint target's visibility is View.GONE, you can also indicates a different margin value to be used using the following attributes:

  • layout_goneMarginStart
  • layout_goneMarginEnd
  • layout_goneMarginLeft
  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_goneMarginBottom

Centering positioning and bias

A useful aspect of ConstraintLayout is in how it deals with "impossible" constrains. For example, if we have something like:

< ...>
             <Button android:id="@+id/button" ...

Unless the ConstraintLayout happens to have the exact same size as the Button, both constraints cannot be satisfied at the same time (both sides cannot be where we want them to be).

Fig. 4 - Centering Positioning

What happens in this case is that the constraints act like opposite forces pulling the widget apart equally (Fig. 4); such that the widget will end up being centered in the parent container. This will apply similarly for vertical constraints.


The default when encountering such opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes:

  • layout_constraintHorizontal_bias
  • layout_constraintVertical_bias

Fig. 5 - Centering Positioning with Bias

For example the following will make the left side with a 30% bias instead of the default 50%, such that the left side will be shorter, with the widget leaning more toward the left side (Fig. 5):

< ...>
             <Button android:id="@+id/button" ...
Using bias, you can craft User Interfaces that will better adapt to screen sizes changes.

Visibility behavior

ConstraintLayout has a specific handling of widgets being marked as View.GONE.

GONE widgets, as usual, are not going to be displayed and are not part of the layout itself (i.e. their actual dimensions will not be changed if marked as GONE).

But in terms of the layout computations, GONE widgets are still part of it, with an important distinction:

  • For the layout pass, their dimension will be considered as if zero (basically, they will be resolved to a point)
  • If they have constraints to other widgets they will still be respected, but any margins will be as if equals to zero

Fig. 6 - Visibility Behavior

This specific behavior allows to build layouts where you can temporarily mark widgets as being GONE, without breaking the layout (Fig. 6), which can be particularly useful when doing simple layout animations.

Note: The margin used will be the margin that B had defined when connecting to A (see Fig. 6 for an example). In some cases, this might not be the margin you want (e.g. A had a 100dp margin to the side of its container, B only a 16dp to A, marking A as gone, B will have a margin of 16dp to the container). For this reason, you can specify an alternate margin value to be used when the connection is to a widget being marked as gone (see the section above about the gone margin attributes).

Dimensions constraints

Minimum dimensions on ConstraintLayout

You can define minimum sizes for the ConstraintLayout itself:

  • android:minWidth set the minimum width for the layout
  • android:minHeight set the minimum height for the layout
Those minimum dimensions will be used by ConstraintLayout when its dimensions are set to WRAP_CONTENT.

Widgets dimension constraints

The dimension of the widgets can be specified by setting the android:layout_width and android:layout_height attributes in 3 different ways:

  • Using a specific dimension (either a literal value such as 123dp or a Dimension reference)
  • Using WRAP_CONTENT, which will ask the widget to compute its own size
  • Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

Fig. 7 - Dimension Constraints
The first two works in a similar fashion as other layouts. The last one will resize the widget in such a way as matching the constraints that are set (see Fig. 7, (a) is wrap_content, (b) is 0dp). If margins are set, they will be taken in account in the computation (Fig. 7, (c) with 0dp).

Important: MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".


You can also define one dimension of a widget as a ratio of the other one. In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT), and set the attribute layout_constraintDimentionRatio to a given ratio. For example:

         <Button android:layout_width="wrap_content"
                   app:layout_constraintDimensionRatio="1:1" />
will set the height of the button to be the same as its width.

The ratio can be expressed either as:

  • a float value, representing a ratio between width and height
  • a ratio in the form "width:height"

You can also use ratio if both dimensions are set to MATCH_CONSTRAINT (0dp). In this case the system sets the largest dimensions the satisfies all constraints and maintains the aspect ratio specified. To constrain one specific side based on the dimensions of another. You can pre append W," or H, to constrain the width or height respectively. For example, If one dimension is constrained by two targets (e.g. width is 0dp and centered on parent) you can indicate which side should be constrained, by adding the letter W (for constraining the width) or H (for constraining the height) in front of the ratio, separated by a comma:

         <Button android:layout_width="0dp"
will set the height of the button following a 16:9 ratio, while the width of the button will match the constraints to parent.


Chains provide group-like behavior in a single axis (horizontally or vertically). The other axis can be constrained independently.

Creating a chain

A set of widgets are considered a chain if they a linked together via a bi-directional connection (see Fig. 8, showing a minimal chain, with two widgets).

Fig. 8 - Chain

Chain heads

Chains are controlled by attributes set on the first element of the chain (the "head" of the chain):

Fig. 9 - Chain Head

The head is the left-most widget for horizontal chains, and the top-most widget for vertical chains.

Margins in chains

If margins are specified on connections, they will be taken in account. In the case of spread chains, margins will be deducted from the allocated space.

Chain Style

When setting the attribute layout_constraintHorizontal_chainStyle or layout_constraintVertical_chainStyle on the first element of a chain, the behavior of the chain will change according to the specified style (default is CHAIN_SPREAD).

  • CHAIN_SPREAD -- the elements will be spread out (default style)
  • Weighted chain -- in CHAIN_SPREAD mode, if some widgets are set to MATCH_CONSTRAINT, they will split the available space
  • CHAIN_SPREAD_INSIDE -- similar, but the endpoints of the chain will not be spread out
  • CHAIN_PACKED -- the elements of the chain will be packed together. The horizontal or vertical bias attribute of the child will then affect the positioning of the packed elements

Fig. 10 - Chains Styles

Weighted chains

The default behavior of a chain is to spread the elements equally in the available space. If one or more elements are using MATCH_CONSTRAINT, they will use the available empty space (equally divided among themselves). The attribute layout_constraintHorizontal_weight and layout_constraintVertical_weight will control how the space will be distributed among the elements using MATCH_CONSTRAINT. For exemple, on a chain containing two elements using MATCH_CONSTRAINT, with the first element using a weight of 2 and the second a weight of 1, the space occupied by the first element will be twice that of the second element.

Virtual Helper objects

In addition to the intrinsic capabilities detailed previously, you can also use special helper objects in ConstraintLayout to help you with your layout. Currently, the Guideline object allows you to create Horizontal and Vertical guidelines which are positioned relative to the ConstraintLayout container. Widgets can then be positioned by constraining them to such guidelines.

See also:


Nested classes

class ConstraintLayout.LayoutParams

This class contains the different attributes specifying how a view want to be laid out inside a ConstraintLayout

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

ConstraintLayout(Context context)
ConstraintLayout(Context context, AttributeSet attrs)
ConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void addView(View child, int index, ViewGroup.LayoutParams params)

ConstraintLayout.LayoutParams generateLayoutParams(AttributeSet attrs)

int getMaxHeight()

The maximum height of this view.

int getMaxWidth()
int getMinHeight()

The minimum height of this view.

int getMinWidth()
void onViewAdded(View view)

void onViewRemoved(View view)

void removeView(View view)

void requestLayout()

void setConstraintSet(ConstraintSet set)

Sets a ConstraintSet object to manage constraints.

void setMaxHeight(int value)

Set the max height for this view

void setMaxWidth(int value)

Set the max width for this view

void setMinHeight(int value)

Set the min height for this view

void setMinWidth(int value)

Set the min width for this view

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)

ConstraintLayout.LayoutParams generateDefaultLayoutParams()

ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)

void onLayout(boolean changed, int left, int top, int right, int bottom)

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited methods

From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Public constructors


ConstraintLayout (Context context)

context Context


ConstraintLayout (Context context, 
                AttributeSet attrs)

context Context
attrs AttributeSet


ConstraintLayout (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

context Context
attrs AttributeSet
defStyleAttr int

Public methods


void addView (View child, 
                int index, 
                ViewGroup.LayoutParams params)

child View
index int
params ViewGroup.LayoutParams


ConstraintLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

attrs AttributeSet


int getMaxHeight ()

The maximum height of this view.

int The maximum height of this view

See also:


int getMaxWidth ()



int getMinHeight ()

The minimum height of this view.

int The minimum height of this view

See also:


int getMinWidth ()



void onViewAdded (View view)

view View


void onViewRemoved (View view)

view View


void removeView (View view)

view View


void requestLayout ()


void setConstraintSet (ConstraintSet set)

Sets a ConstraintSet object to manage constraints. The ConstraintSet overrides LayoutParams of child views.

set ConstraintSet: Layout children using ConstraintSet


void setMaxHeight (int value)

Set the max height for this view


void setMaxWidth (int value)

Set the max width for this view


void setMinHeight (int value)

Set the min height for this view


void setMinWidth (int value)

Set the min width for this view

Protected methods


boolean checkLayoutParams (ViewGroup.LayoutParams p)

p ViewGroup.LayoutParams


ConstraintLayout.LayoutParams generateDefaultLayoutParams ()



ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

p ViewGroup.LayoutParams


void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

changed boolean
left int
top int
right int
bottom int


void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

widthMeasureSpec int
heightMeasureSpec int
This site uses cookies to store your preferences for site-specific language and display options.


This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.