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