public class ConstraintSet


Defines a set of constraints to be used with ConstraintLayout.

ConstraintSet enables you create and save constraints and apply them to an existing ConstraintLayout. For details about constraint behaviour, see ConstraintLayout.

ConstraintsSet can be created in various ways:

  • Manually — c = new ConstraintSet(); c.connect(...);
  • From an R.layout.* object — c.clone(context, R.layout.layout1);
  • From a ConstraintLayoutc.clone(constraintLayout);

Example code:

import android.content.Context;
     import android.os.Bundle;
     import android.support.constraint.ConstraintLayout;
     import android.support.constraint.ConstraintSet;
     import android.support.transition.TransitionManager;
     import android.support.v7.app.AppCompatActivity;
     import android.view.View;

     public class MainActivity extends AppCompatActivity {
         ConstraintSet mConstraintSet1 = new ConstraintSet(); // Create a ConstraintSet.
         ConstraintSet mConstraintSet2 = new ConstraintSet(); // Create a ConstraintSet.
         ConstraintLayout mConstraintLayout; // Cache the ConstraintLayout.
         boolean mOld = true;


         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             Context context = this;
             mConstraintSet2.clone(context, R.layout.state2); // Get constraints from layout.
             setContentView(R.layout.state1);
             mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main);
             mConstraintSet1.clone(mConstraintLayout); // Get constraints from ConstraintSet.
         }

         public void foo(View view) {
             TransitionManager.beginDelayedTransition(mConstraintLayout);
             if (mOld = !mOld) {
                 mConstraintSet1.applyTo(mConstraintLayout); // Set new constraints.
             }  else {
                 mConstraintSet2.applyTo(mConstraintLayout); // Set new constraints.
             }
         }
     }

Summary

Nested types

Constants

static final int

The baseline of the text in a view.

static final int
BOTTOM = 4

The bottom side of a view.

static final int

Chain packed style

static final int

Chain spread style

static final int

Chain spread inside style

static final int

Circle reference from a view.

static final int
END = 7

The right side of a view in left to right languages.

static final int
GONE = 8

This view is gone, and will not take any space for layout purposes.

static final int

The horizontal orientation.

static final int

Used to create a horizontal create guidelines.

static final int

This view is invisible, but it still takes up space for layout purposes.

static final int
LEFT = 1

The left side of a view.

static final int

Dimension will be controlled by constraints

static final int
static final int

Calculate the size of a view in 0 dp by reducing the constrains gaps as much as possible

static final int

How to calculate the size of a view in 0 dp by using its wrap_content size

static final int

References the id of the parent.

static final int
RIGHT = 2

The right side of a view.

static final int
static final int
static final int
static final int
static final int
static final int
START = 6

The left side of a view in left to right languages.

static final int
TOP = 3

The top of a view.

static final int
UNSET = -1

Used to indicate a parameter is cleared or not set

static final int

The vertical orientation.

static final int

Used to create a vertical create guidelines. see create

static final int
static final int
static final int

This view is visible.

static final int

Dimension will set by the view's content

Public constructors

Public methods

void
addColorAttributes(String[] attributeName)

Add attribute of type Color

void
addFloatAttributes(String[] attributeName)

Add attribute of type float

void
addIntAttributes(String[] attributeName)

Add attribute of type Int

void
addStringAttributes(String[] attributeName)

Add attribute of type string

void
addToHorizontalChain(int viewId, int leftId, int rightId)

Adds a view to a horizontal chain.

void
addToHorizontalChainRTL(int viewId, int leftId, int rightId)

Adds a view to a horizontal chain.

void
addToVerticalChain(int viewId, int topId, int bottomId)

Adds a view to a vertical chain.

void

Apply custom attributes alone

void

Get the delta form the ConstraintSet and aplly to this

void
applyTo(ConstraintLayout constraintLayout)

Apply the constraints to a ConstraintLayout.

void
applyToHelper(
    ConstraintHelper helper,
    ConstraintWidget child,
    ConstraintLayout.LayoutParams layoutParams,
    SparseArray<ConstraintWidget> mapIdToWidget
)

Apply Layout to Helper widget

void

Fill in a ConstraintLayout LayoutParam based on the id.

void

Apply the constraints to a ConstraintLayout.

static ConstraintSet.Constraint
buildDelta(Context context, XmlPullParser parser)

Used to read a ConstraintDelta

void
center(
    int centerID,
    int firstID,
    int firstSide,
    int firstMargin,
    int secondId,
    int secondSide,
    int secondMargin,
    float bias
)

Center widget between the other two widgets.

void
centerHorizontally(int viewId, int toView)

Centers the view horizontally relative to toView's position.

void
centerHorizontally(
    int centerID,
    int leftId,
    int leftSide,
    int leftMargin,
    int rightId,
    int rightSide,
    int rightMargin,
    float bias
)

Centers the widget horizontally to the left and right side on another widgets sides.

void
centerHorizontallyRtl(int viewId, int toView)

Centers the view horizontally relative to toView's position.

void
centerHorizontallyRtl(
    int centerID,
    int startId,
    int startSide,
    int startMargin,
    int endId,
    int endSide,
    int endMargin,
    float bias
)

Centers the widgets horizontally to the left and right side on another widgets sides.

void
centerVertically(int viewId, int toView)

Centers the view vertically relative to toView's position.

void
centerVertically(
    int centerID,
    int topId,
    int topSide,
    int topMargin,
    int bottomId,
    int bottomSide,
    int bottomMargin,
    float bias
)

Centers the widgets vertically to the top and bottom side on another widgets sides.

void
clear(int viewId)

Remove all constraints from this view.

void
clear(int viewId, int anchor)

Remove a constraint from this view.

void
clone(ConstraintLayout constraintLayout)

Copy the layout parameters of a ConstraintLayout.

void
clone(Constraints constraints)

Copy the layout parameters of a ConstraintLayout.

void

Copy the constraints from a layout.

void
clone(Context context, int constraintLayoutId)

Copy the constraints from a layout.

void
connect(int startID, int startSide, int endID, int endSide)

Create a constraint between two widgets.

void
connect(int startID, int startSide, int endID, int endSide, int margin)

Create a constraint between two widgets.

void
constrainCircle(int viewId, int id, int radius, float angle)

Constrain the view on a circle constraint

void
constrainDefaultHeight(int viewId, int height)

Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

void
constrainDefaultWidth(int viewId, int width)

Sets how the width is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

void
constrainHeight(int viewId, int height)

Sets the height of the view.

void
constrainMaxHeight(int viewId, int height)

Sets the maximum height of the view.

void
constrainMaxWidth(int viewId, int width)

Sets the maximum width of the view.

void
constrainMinHeight(int viewId, int height)

Sets the height of the view.

void
constrainMinWidth(int viewId, int width)

Sets the width of the view.

void
constrainPercentHeight(int viewId, float percent)

Sets the height of the view as a percentage of the parent.

void
constrainPercentWidth(int viewId, float percent)

Sets the width of the view as a percentage of the parent.

void
constrainWidth(int viewId, int width)

Sets the width of the view.

void
constrainedHeight(int viewId, boolean constrained)

Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

void
constrainedWidth(int viewId, boolean constrained)

Sets how the width is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

void
create(int guidelineID, int orientation)

Creates a ConstraintLayout virtual object.

void
createBarrier(int id, int direction, int margin, int[] referenced)

Creates a ConstraintLayout Barrier object.

void
createHorizontalChain(
    int leftId,
    int leftSide,
    int rightId,
    int rightSide,
    int[] chainIds,
    float[] weights,
    int style
)

Spaces a set of widgets horizontally between the view startID and endId.

void
createHorizontalChainRtl(
    int startId,
    int startSide,
    int endId,
    int endSide,
    int[] chainIds,
    float[] weights,
    int style
)

Spaces a set of widgets horizontal between the view startID and endId.

void
createVerticalChain(
    int topId,
    int topSide,
    int bottomId,
    int bottomSide,
    int[] chainIds,
    float[] weights,
    int style
)

Spaces a set of widgets vertically between the view topId and bottomId.

void
dump(MotionScene scene, int[] ids)

Dump the contents

boolean
getApplyElevation(int viewId)

return with the constraint set will apply elevation for the specified view.

ConstraintSet.Constraint
getConstraint(int id)
HashMap<StringConstraintAttribute>
int
getHeight(int viewId)

Get the height set in the view

int[]
ConstraintSet.Constraint
getParameters(int mId)
int[]

get the reference id's of a helper.

String[]

Get the types associated with this ConstraintSet The types mechanism allows you to tag the constraint set with a series of string to define properties of a ConstraintSet

int
getVisibility(int viewId)

Get the visibility flag set in this view

int
getVisibilityMode(int viewId)

ConstraintSet will not setVisibility.

int
getWidth(int viewId)

Get the width set in the view

boolean

Enforce id are required for all ConstraintLayout children to use ConstraintSet. default = true;

boolean

If true perform validation checks when parsing ConstraintSets This will slow down parsing and should only be used for debugging

void
load(Context context, XmlPullParser parser)

Load a constraint set from a constraintSet.xml file

void
load(Context context, int resourceId)

Load a constraint set from a constraintSet.xml file.

boolean
matchesLabels(String[] types)

Test if the list of strings matches labels defined on this constraintSet

void

Parse color

void

Parse floats

void

Parse int

void

Parse string

void
readFallback(ConstraintLayout constraintLayout)

This will copy Constraints from the ConstraintLayout if it does not have parameters

void

This will copy Constraints from the ConstraintSet

void
removeAttribute(String attributeName)

Remove the attribute

void

Removes a view from a horizontal chain.

void

Removes a view from a vertical chain.

void
setAlpha(int viewId, float alpha)

Adjust the alpha of a view.

void
setApplyElevation(int viewId, boolean apply)

set if elevation will be applied to the view.

void
setBarrierType(int id, int type)

SEt tye type of barier

void
setColorValue(int viewId, String attributeName, int value)

Set the value of an attribute of type color

void
setDimensionRatio(int viewId, String ratio)

Constrains the views aspect ratio.

void
setEditorAbsoluteX(int viewId, int position)
void
setEditorAbsoluteY(int viewId, int position)
void
setElevation(int viewId, float elevation)

Adjust the elevation of a view.

void
setFloatValue(int viewId, String attributeName, float value)

Set the value of an attribute of type float

void
setForceId(boolean forceId)

Enforce id are required for all ConstraintLayout children to use ConstraintSet. default = true;

void
setGoneMargin(int viewId, int anchor, int value)

Sets the gone margin.

void
setGuidelineBegin(int guidelineID, int margin)

Set the guideline's distance form the top or left edge.

void
setGuidelineEnd(int guidelineID, int margin)

Set a guideline's distance to end.

void
setGuidelinePercent(int guidelineID, float ratio)

Set a Guideline's percent.

void
setHorizontalBias(int viewId, float bias)

Adjust the horizontal bias of the view (used with views constrained on left and right).

void
setHorizontalChainStyle(int viewId, int chainStyle)

How the elements of the horizontal chain will be positioned. if the dimension behaviour is set to MATCH_CONSTRAINT.

void
setHorizontalWeight(int viewId, float weight)

The child's weight that we can use to distribute the available horizontal space in a chain, if the dimension behaviour is set to MATCH_CONSTRAINT

void
setIntValue(int viewId, String attributeName, int value)

Set the value of an attribute of type int

void
setLayoutWrapBehavior(int viewId, int behavior)

Sets the wrap behavior of the widget in the parent's wrap computation

void
setMargin(int viewId, int anchor, int value)

Sets the margin.

void
setReferencedIds(int id, int[] referenced)

sets the reference id's of a barrier.

void
setRotation(int viewId, float rotation)

Adjust the post-layout rotation about the Z axis of a view.

void
setRotationX(int viewId, float rotationX)

Adjust the post-layout rotation about the X axis of a view.

void
setRotationY(int viewId, float rotationY)

Adjust the post-layout rotation about the Y axis of a view.

void
setScaleX(int viewId, float scaleX)

Adjust the post-layout scale in X of a view.

void
setScaleY(int viewId, float scaleY)

Adjust the post-layout scale in Y of a view.

void

Set the types associated with this ConstraintSet The types mechanism allows you to tag the constraint set with a series of string to define properties of a ConstraintSet

void
setStateLabelsList(String[] types)

Set the types associated with this ConstraintSet The types mechanism allows you to tag the constraint set with a series of string to define properties of a ConstraintSet

void
setStringValue(int viewId, String attributeName, String value)

Set the value of an attribute of type string

void
setTransformPivot(
    int viewId,
    float transformPivotX,
    float transformPivotY
)

Set X,Y location of the pivot point around which the view will rotate and scale. use Float.NaN to clear the pivot value.

void
setTransformPivotX(int viewId, float transformPivotX)

Set X location of the pivot point around which the view will rotate and scale. use Float.NaN to clear the pivot value.

void
setTransformPivotY(int viewId, float transformPivotY)

Set Y location of the pivot point around which the view will rotate and scale. use Float.NaN to clear the pivot value.

void
setTranslation(int viewId, float translationX, float translationY)

Adjust the post-layout translation of a view.

void
setTranslationX(int viewId, float translationX)

Adjust the post-layout X translation of a view.

void
setTranslationY(int viewId, float translationY)

Adjust the post-layout Y translation of a view.

void
setTranslationZ(int viewId, float translationZ)

Adjust the translation in Z of a view.

void
setValidateOnParse(boolean validate)

If true perform validation checks when parsing ConstraintSets This will slow down parsing and should only be used for debugging

void
setVerticalBias(int viewId, float bias)

Adjust the vertical bias of the view (used with views constrained on left and right).

void
setVerticalChainStyle(int viewId, int chainStyle)

How the elements of the vertical chain will be positioned. in a chain, if the dimension behaviour is set to MATCH_CONSTRAINT

void
setVerticalWeight(int viewId, float weight)

The child's weight that we can use to distribute the available vertical space in a chain, if the dimension behaviour is set to MATCH_CONSTRAINT

void
setVisibility(int viewId, int visibility)

Adjust the visibility of a view.

void
setVisibilityMode(int viewId, int visibilityMode)

ConstraintSet will not setVisibility.

void
writeState(Writer writer, ConstraintLayout layout, int flags)

Write the state to a Writer

Constants

BASELINE

Added in 2.2.1
public static final int BASELINE = 5

The baseline of the text in a view.

BOTTOM

Added in 2.2.1
public static final int BOTTOM = 4

The bottom side of a view.

CHAIN_PACKED

Added in 2.2.1
public static final int CHAIN_PACKED = 2

Chain packed style

CHAIN_SPREAD

Added in 2.2.1
public static final int CHAIN_SPREAD = 0

Chain spread style

CHAIN_SPREAD_INSIDE

Added in 2.2.1
public static final int CHAIN_SPREAD_INSIDE = 1

Chain spread inside style

CIRCLE_REFERENCE

Added in 2.2.1
public static final int CIRCLE_REFERENCE = 8

Circle reference from a view.

END

Added in 2.2.1
public static final int END = 7

The right side of a view in left to right languages. In right to left languages it corresponds to the left side of the view

GONE

Added in 2.2.1
public static final int GONE = 8

This view is gone, and will not take any space for layout purposes. Use with setVisibility and android:visibility.

HORIZONTAL

Added in 2.2.1
public static final int HORIZONTAL = 0

The horizontal orientation.

HORIZONTAL_GUIDELINE

Added in 2.2.1
public static final int HORIZONTAL_GUIDELINE = 0

Used to create a horizontal create guidelines.

INVISIBLE

Added in 2.2.1
public static final int INVISIBLE = 4

This view is invisible, but it still takes up space for layout purposes. Use with setVisibility and android:visibility.

LEFT

Added in 2.2.1
public static final int LEFT = 1

The left side of a view.

MATCH_CONSTRAINT

Added in 2.2.1
public static final int MATCH_CONSTRAINT = 0

Dimension will be controlled by constraints

MATCH_CONSTRAINT_PERCENT

Added in 2.2.1
public static final int MATCH_CONSTRAINT_PERCENT = 2

MATCH_CONSTRAINT_SPREAD

Added in 2.2.1
public static final int MATCH_CONSTRAINT_SPREAD = 0

Calculate the size of a view in 0 dp by reducing the constrains gaps as much as possible

MATCH_CONSTRAINT_WRAP

Added in 2.2.1
public static final int MATCH_CONSTRAINT_WRAP = 1

How to calculate the size of a view in 0 dp by using its wrap_content size

PARENT_ID

Added in 2.2.1
public static final int PARENT_ID = 0

References the id of the parent. Used in:

RIGHT

Added in 2.2.1
public static final int RIGHT = 2

The right side of a view.

ROTATE_LEFT_OF_PORTRATE

Added in 2.2.1
public static final int ROTATE_LEFT_OF_PORTRATE = 4

ROTATE_NONE

Added in 2.2.1
public static final int ROTATE_NONE = 0

ROTATE_PORTRATE_OF_LEFT

Added in 2.2.1
public static final int ROTATE_PORTRATE_OF_LEFT = 2

ROTATE_PORTRATE_OF_RIGHT

Added in 2.2.1
public static final int ROTATE_PORTRATE_OF_RIGHT = 1

ROTATE_RIGHT_OF_PORTRATE

Added in 2.2.1
public static final int ROTATE_RIGHT_OF_PORTRATE = 3

START

Added in 2.2.1
public static final int START = 6

The left side of a view in left to right languages. In right to left languages it corresponds to the right side of the view

TOP

Added in 2.2.1
public static final int TOP = 3

The top of a view.

UNSET

Added in 2.2.1
public static final int UNSET = -1

Used to indicate a parameter is cleared or not set

VERTICAL

Added in 2.2.1
public static final int VERTICAL = 1

The vertical orientation.

VERTICAL_GUIDELINE

Added in 2.2.1
public static final int VERTICAL_GUIDELINE = 1

Used to create a vertical create guidelines. see create

VISIBILITY_MODE_IGNORE

Added in 2.2.1
public static final int VISIBILITY_MODE_IGNORE = 1

VISIBILITY_MODE_NORMAL

Added in 2.2.1
public static final int VISIBILITY_MODE_NORMAL = 0

VISIBLE

Added in 2.2.1
public static final int VISIBLE = 0

This view is visible. Use with setVisibility and android:visibility.

WRAP_CONTENT

Added in 2.2.1
public static final int WRAP_CONTENT = -2

Dimension will set by the view's content

Public fields

derivedState

Added in 2.2.1
public String derivedState

mIdString

Added in 2.2.1
public String mIdString

mRotate

Added in 2.2.1
public int mRotate

Public constructors

ConstraintSet

Added in 2.2.1
public ConstraintSet()

Public methods

addColorAttributes

public void addColorAttributes(String[] attributeName)

Add attribute of type Color

Parameters
String[] attributeName

addFloatAttributes

public void addFloatAttributes(String[] attributeName)

Add attribute of type float

Parameters
String[] attributeName

addIntAttributes

public void addIntAttributes(String[] attributeName)

Add attribute of type Int

Parameters
String[] attributeName

addStringAttributes

public void addStringAttributes(String[] attributeName)

Add attribute of type string

Parameters
String[] attributeName

addToHorizontalChain

Added in 2.2.1
public void addToHorizontalChain(int viewId, int leftId, int rightId)

Adds a view to a horizontal chain.

Parameters
int viewId

view to add

int leftId

view in chain to the left

int rightId

view in chain to the right

addToHorizontalChainRTL

Added in 2.2.1
public void addToHorizontalChainRTL(int viewId, int leftId, int rightId)

Adds a view to a horizontal chain.

Parameters
int viewId

view to add

int leftId

view to the start side

int rightId

view to the end side

addToVerticalChain

Added in 2.2.1
public void addToVerticalChain(int viewId, int topId, int bottomId)

Adds a view to a vertical chain.

Parameters
int viewId

view to add to a vertical chain

int topId

view above.

int bottomId

view below

applyCustomAttributes

Added in 2.2.1
public void applyCustomAttributes(ConstraintLayout constraintLayout)

Apply custom attributes alone

Parameters
ConstraintLayout constraintLayout

applyDeltaFrom

Added in 2.2.1
public void applyDeltaFrom(ConstraintSet cs)

Get the delta form the ConstraintSet and aplly to this

Parameters
ConstraintSet cs

applyTo

Added in 2.2.1
public void applyTo(ConstraintLayout constraintLayout)

Apply the constraints to a ConstraintLayout.

Parameters
ConstraintLayout constraintLayout

to be modified

applyToHelper

Added in 2.2.1
public void applyToHelper(
    ConstraintHelper helper,
    ConstraintWidget child,
    ConstraintLayout.LayoutParams layoutParams,
    SparseArray<ConstraintWidget> mapIdToWidget
)

Apply Layout to Helper widget

Parameters
ConstraintHelper helper
ConstraintWidget child
ConstraintLayout.LayoutParams layoutParams
SparseArray<ConstraintWidget> mapIdToWidget

applyToLayoutParams

Added in 2.2.1
public void applyToLayoutParams(int id, ConstraintLayout.LayoutParams layoutParams)

Fill in a ConstraintLayout LayoutParam based on the id.

Parameters
int id

Id of the view

ConstraintLayout.LayoutParams layoutParams

LayoutParams to be filled

applyToWithoutCustom

Added in 2.2.1
public void applyToWithoutCustom(ConstraintLayout constraintLayout)

Apply the constraints to a ConstraintLayout.

Parameters
ConstraintLayout constraintLayout

to be modified

buildDelta

Added in 2.2.1
public static ConstraintSet.Constraint buildDelta(Context context, XmlPullParser parser)

Used to read a ConstraintDelta

Parameters
Context context
XmlPullParser parser

center

Added in 2.2.1
public void center(
    int centerID,
    int firstID,
    int firstSide,
    int firstMargin,
    int secondId,
    int secondSide,
    int secondMargin,
    float bias
)

Center widget between the other two widgets. (for sides see: TOP, BOTTOM, START, END, LEFT, RIGHT) Note, sides must be all vertical or horizontal sides.

Parameters
int centerID

ID of the widget to be centered

int firstID

ID of the first widget to connect the left or top of the widget to

int firstSide

the side of the widget to connect to

int firstMargin

the connection margin

int secondId

the ID of the second widget to connect to right or top of the widget to

int secondSide

the side of the widget to connect to

int secondMargin

the connection margin

float bias

the ratio between two connections