ConstraintLayout
public
class
ConstraintLayout
extends ViewGroup
java.lang.Object | ||
↳ | ViewGroup | |
↳ | android.support.constraint.ConstraintLayout |
A ConstraintLayout
is a android.view.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 on enriching its API and capabilities over time.
This documentation will reflect those changes.
There are currently various types of constraints that you can use:
- Relative positioning
- Margins
- Centering positioning
- Circular positioning
- Visibility behavior
- Dimension constraints
- Chains
- Virtual Helpers objects
- Optimizer
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 blocks 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:

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 take a reference id
to another widget, or the parent
(which will reference the parent container, i.e. the ConstraintLayout):
Margins

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 margin 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 indicate 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:
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 PositioningWhat 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.
Bias
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):
See also:
Summary
Nested classes | |
---|---|
class |
ConstraintLayout.LayoutParams
This class contains the different attributes specifying how a view want to be laid out inside
a |
Fields | |
---|---|
protected
ConstraintLayoutStates |
mConstraintLayoutSpec
|
protected
ConstraintWidgetContainer |
mLayoutWidget
|
Public constructors | |
---|---|
ConstraintLayout(Context context)
|
|
ConstraintLayout(Context context, AttributeSet attrs)
|
|
ConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr)
|
|
ConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
|
Public methods | |
---|---|
int
|
getMaxHeight()
The maximum height of this view. |
int
|
getMaxWidth()
|
int
|
getMinHeight()
The minimum height of this view. |
int
|
getMinWidth()
The minimum width of this view. |
int
|
getOptimizationLevel()
Return the current optimization level for the layout resolution |
void
|
loadLayoutDescription(int layoutDescription)
Load a layout description file from the resources. |
void
|
requestLayout()
|
void
|
resolveSystem(int widthMeasureSpec, int heightMeasureSpec)
|
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 |
void
|
setOnConstraintsChanged(ConstraintsChangedListener constraintsChangedListener)
|
void
|
setOptimizationLevel(int level)
Set the optimization for the layout resolution. |
void
|
setState(int id, int screenWidth, int screenHeight)
Set the State of the ConstraintLayout, causing it to load a particular ConstraintSet. |
Protected methods | |
---|---|
void
|
applyConstraintsFromLayoutParams(boolean isInEditMode, View child, ConstraintWidget widget, ConstraintLayout.LayoutParams layoutParams,
|
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)
|
void
|
parseLayoutDescription(int id)
Subclasses can override the handling of layoutDescription |
Inherited methods | |
---|---|
Fields
mLayoutWidget
protected ConstraintWidgetContainer mLayoutWidget
Public constructors
ConstraintLayout
public ConstraintLayout (Context context)
Parameters | |
---|---|
context |
Context |
ConstraintLayout
public ConstraintLayout (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
ConstraintLayout
public ConstraintLayout (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
ConstraintLayout
public ConstraintLayout (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
defStyleRes |
int |
Public methods
getMaxHeight
public int getMaxHeight ()
The maximum height of this view.
Returns | |
---|---|
int |
The maximum height of this view |
See also:
getMaxWidth
public int getMaxWidth ()
Returns | |
---|---|
int |
getMinHeight
public int getMinHeight ()
The minimum height of this view.
Returns | |
---|---|
int |
The minimum height of this view |
See also:
getMinWidth
public int getMinWidth ()
The minimum width of this view.
Returns | |
---|---|
int |
The minimum width of this view |
See also:
getOptimizationLevel
public int getOptimizationLevel ()
Return the current optimization level for the layout resolution
Returns | |
---|---|
int |
the current level |
loadLayoutDescription
public void loadLayoutDescription (int layoutDescription)
Load a layout description file from the resources.
Parameters | |
---|---|
layoutDescription |
int : The resource id, or 0 to reset the layout description.
|
requestLayout
public void requestLayout ()
resolveSystem
public void resolveSystem (int widthMeasureSpec, int heightMeasureSpec)
Parameters | |
---|---|
widthMeasureSpec |
int |
heightMeasureSpec |
int |
setConstraintSet
public void setConstraintSet (ConstraintSet set)
Sets a ConstraintSet object to manage constraints. The ConstraintSet overrides LayoutParams of child views.
Parameters | |
---|---|
set |
ConstraintSet : Layout children using ConstraintSet
|
setMaxHeight
public void setMaxHeight (int value)
Set the max height for this view
setMaxWidth
public void setMaxWidth (int value)
Set the max width for this view
setMinHeight
public void setMinHeight (int value)
Set the min height for this view
setMinWidth
public void setMinWidth (int value)
Set the min width for this view
setOnConstraintsChanged
public void setOnConstraintsChanged (ConstraintsChangedListener constraintsChangedListener)
Parameters | |
---|---|
constraintsChangedListener |
ConstraintsChangedListener |
setOptimizationLevel
public void setOptimizationLevel (int level)
Set the optimization for the layout resolution.
The optimization can be any of the following:
- Optimizer.OPTIMIZATION_NONE
- Optimizer.OPTIMIZATION_STANDARD
- a mask composed of specific optimizations
- Optimizer.OPTIMIZATION_DIRECT
- Optimizer.OPTIMIZATION_BARRIER
- Optimizer.OPTIMIZATION_CHAIN (experimental)
- Optimizer.OPTIMIZATION_DIMENSIONS (experimental)
Parameters | |
---|---|
level |
int : optimization level |
setState
public void setState (int id, int screenWidth, int screenHeight)
Set the State of the ConstraintLayout, causing it to load a particular ConstraintSet. For states with variants the variant with matching width and height constraintSet will be chosen
Parameters | |
---|---|
id |
int : the constraint set state |
screenWidth |
int : the width of the screen in pixels |
screenHeight |
int : the height of the screen in pixels
|
Protected methods
applyConstraintsFromLayoutParams
protected void applyConstraintsFromLayoutParams (boolean isInEditMode, View child, ConstraintWidget widget, ConstraintLayout.LayoutParams layoutParams,idToWidget)
Parameters | |
---|---|
isInEditMode |
boolean |
child |
View |
widget |
ConstraintWidget |
layoutParams |
ConstraintLayout.LayoutParams |
idToWidget |
|
checkLayoutParams
protected boolean checkLayoutParams (ViewGroup.LayoutParams p)
Parameters | |
---|---|
p |
ViewGroup.LayoutParams |
Returns | |
---|---|
boolean |
generateDefaultLayoutParams
protected ConstraintLayout.LayoutParams generateDefaultLayoutParams ()
Returns | |
---|---|
ConstraintLayout.LayoutParams |
generateLayoutParams
protected ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)
Parameters | |
---|---|
p |
ViewGroup.LayoutParams |
Returns | |
---|---|
ViewGroup.LayoutParams |
onLayout
protected void onLayout (boolean changed, int left, int top, int right, int bottom)
Parameters | |
---|---|
changed |
boolean |
left |
int |
top |
int |
right |
int |
bottom |
int |
onMeasure
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
Parameters | |
---|---|
widthMeasureSpec |
int |
heightMeasureSpec |
int |
parseLayoutDescription
protected void parseLayoutDescription (int id)
Subclasses can override the handling of layoutDescription
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.