ConstraintProperties

public class ConstraintProperties


Added in 2.0

ConstraintProperties provides an easy to use api to update the layout params of ConstraintLayout children

Summary

Constants

static final @NonNull int

The baseline of the text in a view.

static final @NonNull int

The bottom side of a view.

static final @NonNull int

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

static final @NonNull int

The left side of a view.

static final @NonNull int

Dimension will be controlled by constraints

static final @NonNull int

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

static final @NonNull int

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

static final @NonNull int

References the id of the parent.

static final @NonNull int

The right side of a view.

static final @NonNull int

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

static final @NonNull int

The top of a view.

static final @NonNull int

Used to indicate a parameter is cleared or not set

static final @NonNull int

Dimension will set by the view's content

Public constructors

Public methods

@NonNull ConstraintProperties
addToHorizontalChain(@NonNull int leftId, @NonNull int rightId)

Adds the view to a horizontal chain.

@NonNull ConstraintProperties
addToHorizontalChainRTL(@NonNull int leftId, @NonNull int rightId)

Adds the view to a horizontal chain using RTL attributes.

@NonNull ConstraintProperties
addToVerticalChain(@NonNull int topId, @NonNull int bottomId)

Adds a view to a vertical chain.

@NonNull ConstraintProperties
alpha(@NonNull float alpha)

Adjust the alpha of a view.

@NonNull void
@NonNull ConstraintProperties
center(
    @NonNull int firstID,
    @NonNull int firstSide,
    @NonNull int firstMargin,
    @NonNull int secondId,
    @NonNull int secondSide,
    @NonNull int secondMargin,
    @NonNull float bias
)

Center view between the other two widgets.

@NonNull ConstraintProperties
centerHorizontally(
    @NonNull int leftId,
    @NonNull int leftSide,
    @NonNull int leftMargin,
    @NonNull int rightId,
    @NonNull int rightSide,
    @NonNull int rightMargin,
    @NonNull float bias
)

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

@NonNull ConstraintProperties

Centers the view horizontally relative to toView's position.

@NonNull ConstraintProperties
centerHorizontallyRtl(
    @NonNull int startId,
    @NonNull int startSide,
    @NonNull int startMargin,
    @NonNull int endId,
    @NonNull int endSide,
    @NonNull int endMargin,
    @NonNull float bias
)

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

@NonNull ConstraintProperties

Centers the view horizontally relative to toView's position.

@NonNull ConstraintProperties
centerVertically(
    @NonNull int topId,
    @NonNull int topSide,
    @NonNull int topMargin,
    @NonNull int bottomId,
    @NonNull int bottomSide,
    @NonNull int bottomMargin,
    @NonNull float bias
)

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

@NonNull ConstraintProperties

Centers the view vertically relative to toView's position.

@NonNull ConstraintProperties
connect(
    @NonNull int startSide,
    @NonNull int endID,
    @NonNull int endSide,
    @NonNull int margin
)

Create a constraint between two widgets.

@NonNull ConstraintProperties

Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

@NonNull ConstraintProperties

Sets how the width is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

@NonNull ConstraintProperties
constrainHeight(@NonNull int height)

Sets the height of the view.

@NonNull ConstraintProperties

Sets the maximum height of the view.

@NonNull ConstraintProperties

Sets the maximum width of the view.

@NonNull ConstraintProperties

Sets the minimum height of the view.

@NonNull ConstraintProperties

Sets the minimum width of the view.

@NonNull ConstraintProperties
constrainWidth(@NonNull int width)

Sets the width of the view.

@NonNull ConstraintProperties

Constrains the views aspect ratio.

@NonNull ConstraintProperties
elevation(@NonNull float elevation)

Set the elevation of a view.

@NonNull ConstraintProperties
goneMargin(@NonNull int anchor, @NonNull int value)

Sets the gone margin.

@NonNull ConstraintProperties
horizontalBias(@NonNull float bias)

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

@NonNull ConstraintProperties
horizontalChainStyle(@NonNull int chainStyle)

How the elements of the horizontal chain will be positioned.

@NonNull ConstraintProperties
horizontalWeight(@NonNull 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

@NonNull ConstraintProperties
margin(@NonNull int anchor, @NonNull int value)

Sets the margin.

@NonNull ConstraintProperties

Remove a constraint from this view.

@NonNull ConstraintProperties

Removes a view from a vertical chain.

@NonNull ConstraintProperties

Removes a view from a vertical chain.

@NonNull ConstraintProperties
rotation(@NonNull float rotation)

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

@NonNull ConstraintProperties
rotationX(@NonNull float rotationX)

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

@NonNull ConstraintProperties
rotationY(@NonNull float rotationY)

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

@NonNull ConstraintProperties
scaleX(@NonNull float scaleX)

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

@NonNull ConstraintProperties
scaleY(@NonNull float scaleY)

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

@NonNull ConstraintProperties
transformPivot(
    @NonNull float transformPivotX,
    @NonNull float transformPivotY
)

Set X and Y location of the pivot point around which the view will rotate and scale.

@NonNull ConstraintProperties
transformPivotX(@NonNull float transformPivotX)

Set X location of the pivot point around which the view will rotate and scale.

@NonNull ConstraintProperties
transformPivotY(@NonNull float transformPivotY)

Set Y location of the pivot point around which the view will rotate and scale.

@NonNull ConstraintProperties
translation(@NonNull float translationX, @NonNull float translationY)

Adjust the post-layout X and Y translation of a view.

@NonNull ConstraintProperties
translationX(@NonNull float translationX)

Adjust the post-layout X translation of a view.

@NonNull ConstraintProperties
translationY(@NonNull float translationY)

Adjust the post-layout Y translation of a view.

@NonNull ConstraintProperties
translationZ(@NonNull float translationZ)

Adjust the post-layout translation in Z of a view.

@NonNull ConstraintProperties
verticalBias(@NonNull float bias)

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

@NonNull ConstraintProperties
verticalChainStyle(@NonNull int chainStyle)

How the elements of the vertical chain will be positioned.

@NonNull ConstraintProperties
verticalWeight(@NonNull 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

@NonNull ConstraintProperties
visibility(@NonNull int visibility)

Adjust the visibility of a view.

Constants

BASELINE

@NonNull
public static final @NonNull int BASELINE

The baseline of the text in a view.

BOTTOM

@NonNull
public static final @NonNull int BOTTOM

The bottom side of a view.

END

@NonNull
public static final @NonNull int END

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

LEFT

@NonNull
public static final @NonNull int LEFT

The left side of a view.

MATCH_CONSTRAINT

@NonNull
public static final @NonNull int MATCH_CONSTRAINT

Dimension will be controlled by constraints

MATCH_CONSTRAINT_SPREAD

@NonNull
public static final @NonNull int MATCH_CONSTRAINT_SPREAD

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

MATCH_CONSTRAINT_WRAP

@NonNull
public static final @NonNull int MATCH_CONSTRAINT_WRAP

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

PARENT_ID

@NonNull
public static final @NonNull int PARENT_ID

References the id of the parent.

RIGHT

@NonNull
public static final @NonNull int RIGHT

The right side of a view.

START

@NonNull
public static final @NonNull int START

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

@NonNull
public static final @NonNull int TOP

The top of a view.

UNSET

@NonNull
public static final @NonNull int UNSET

Used to indicate a parameter is cleared or not set

WRAP_CONTENT

@NonNull
public static final @NonNull int WRAP_CONTENT

Dimension will set by the view's content

Public constructors

ConstraintProperties

public ConstraintProperties(@NonNull View view)

Public methods

addToHorizontalChain

@NonNull
public ConstraintProperties addToHorizontalChain(@NonNull int leftId, @NonNull int rightId)

Adds the view to a horizontal chain.

Parameters
@NonNull int leftId

id of the view in chain to the left

@NonNull int rightId

id of the view in chain to the right

Returns
ConstraintProperties

this

addToHorizontalChainRTL

@NonNull
public ConstraintProperties addToHorizontalChainRTL(@NonNull int leftId, @NonNull int rightId)

Adds the view to a horizontal chain using RTL attributes.

Parameters
@NonNull int leftId

id of the view in chain to the left

@NonNull int rightId

id of the view in chain to the right

Returns
ConstraintProperties

this

addToVerticalChain

@NonNull
public ConstraintProperties addToVerticalChain(@NonNull int topId, @NonNull int bottomId)

Adds a view to a vertical chain.

Parameters
@NonNull int topId

view above.

@NonNull int bottomId

view below

Returns
ConstraintProperties

this

alpha

@NonNull
public ConstraintProperties alpha(@NonNull float alpha)

Adjust the alpha of a view.

the alpha

Returns
ConstraintProperties

this

apply

@NonNull
public void apply()

center

@NonNull
public ConstraintProperties center(
    @NonNull int firstID,
    @NonNull int firstSide,
    @NonNull int firstMargin,
    @NonNull int secondId,
    @NonNull int secondSide,
    @NonNull int secondMargin,
    @NonNull float bias
)

Center view between the other two widgets.

Parameters
@NonNull int firstID

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

@NonNull int firstSide

the side of the widget to connect to

@NonNull int firstMargin

the connection margin

@NonNull int secondId

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

@NonNull int secondSide

the side of the widget to connect to

@NonNull int secondMargin

the connection margin

@NonNull float bias

the ratio between two connections

Returns
ConstraintProperties

this

centerHorizontally

@NonNull
public ConstraintProperties centerHorizontally(
    @NonNull int leftId,
    @NonNull int leftSide,
    @NonNull int leftMargin,
    @NonNull int rightId,
    @NonNull int rightSide,
    @NonNull int rightMargin,
    @NonNull float bias
)

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

Parameters
@NonNull int leftId

The Id of the widget on the left side

@NonNull int leftSide

The side of the leftId widget to connect to

@NonNull int leftMargin

The margin on the left side

@NonNull int rightId

The Id of the widget on the right side

@NonNull int rightSide

The side of the rightId widget to connect to

@NonNull int rightMargin

The margin on the right side

@NonNull float bias

The ratio of the space on the left vs. right sides 0.5 is centered (default)

Returns
ConstraintProperties

this

centerHorizontally

@NonNull
public ConstraintProperties centerHorizontally(@NonNull int toView)

Centers the view horizontally relative to toView's position.

Parameters
@NonNull int toView

ID of view to center on (or in)

Returns
ConstraintProperties

this

centerHorizontallyRtl

@NonNull
public ConstraintProperties centerHorizontallyRtl(
    @NonNull int startId,
    @NonNull int startSide,
    @NonNull int startMargin,
    @NonNull int endId,
    @NonNull int endSide,
    @NonNull int endMargin,
    @NonNull float bias
)

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

Parameters
@NonNull int startId

The Id of the widget on the start side (left in non rtl languages)

@NonNull int startSide

The side of the startId widget to connect to

@NonNull int startMargin

The margin on the start side

@NonNull int endId

The Id of the widget on the start side (left in non rtl languages)

@NonNull int endSide

The side of the endId widget to connect to

@NonNull int endMargin

The margin on the end side

@NonNull float bias

The ratio of the space on the start vs end side 0.5 is centered (default)

Returns
ConstraintProperties

this

centerHorizontallyRtl

@NonNull
public ConstraintProperties centerHorizontallyRtl(@NonNull int toView)

Centers the view horizontally relative to toView's position.

Parameters
@NonNull int toView

ID of view to center on (or in)

Returns
ConstraintProperties

this

centerVertically

@NonNull
public ConstraintProperties centerVertically(
    @NonNull int topId,
    @NonNull int topSide,
    @NonNull int topMargin,
    @NonNull int bottomId,
    @NonNull int bottomSide,
    @NonNull int bottomMargin,
    @NonNull float bias
)

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

Parameters
@NonNull int topId

The Id of the widget on the top side

@NonNull int topSide

The side of the leftId widget to connect to

@NonNull int topMargin

The margin on the top side

@NonNull int bottomId

The Id of the widget on the bottom side

@NonNull int bottomSide

The side of the bottomId widget to connect to

@NonNull int bottomMargin

The margin on the bottom side

@NonNull float bias

The ratio of the space on the top vs. bottom sides 0.5 is centered (default)

Returns
ConstraintProperties

this

centerVertically

@NonNull
public ConstraintProperties centerVertically(@NonNull int toView)

Centers the view vertically relative to toView's position.

Parameters
@NonNull int toView

ID of view to center on (or in)

Returns
ConstraintProperties

this

connect

@NonNull
public ConstraintProperties connect(
    @NonNull int startSide,
    @NonNull int endID,
    @NonNull int endSide,
    @NonNull int margin
)

Create a constraint between two widgets.

Parameters
@NonNull int startSide

the side of the widget to constrain

@NonNull int endID

the id of the widget to constrain to

@NonNull int endSide

the side of widget to constrain to

@NonNull int margin

the margin to constrain (margin must be positive)

constrainDefaultHeight

@NonNull
public ConstraintProperties constrainDefaultHeight(@NonNull int height)

Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD. Default is spread.

Parameters
@NonNull int height

MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD

Returns
ConstraintProperties

this

constrainDefaultWidth

@NonNull
public ConstraintProperties constrainDefaultWidth(@NonNull int width)

Sets how the width is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD. Default is spread.

Parameters
@NonNull int width

MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD

Returns
ConstraintProperties

this

constrainHeight

@NonNull
public ConstraintProperties constrainHeight(@NonNull int height)

Sets the height of the view.

Parameters
@NonNull int height

the height of the view

Returns
ConstraintProperties

this

constrainMaxHeight

@NonNull
public ConstraintProperties constrainMaxHeight(@NonNull int height)

Sets the maximum height of the view. It is a dimension, It is only applicable if height is #MATCH_CONSTRAINT}.

Parameters
@NonNull int height

the maximum height of the view

Returns
ConstraintProperties

this

constrainMaxWidth

@NonNull
public ConstraintProperties constrainMaxWidth(@NonNull int width)

Sets the maximum width of the view. It is a dimension, It is only applicable if height is #MATCH_CONSTRAINT}.

Parameters
@NonNull int width

the maximum width of the view

Returns
ConstraintProperties

this

constrainMinHeight

@NonNull
public ConstraintProperties constrainMinHeight(@NonNull int height)

Sets the minimum height of the view. It is a dimension, It is only applicable if height is #MATCH_CONSTRAINT}.

Parameters
@NonNull int height

the minimum height of the view

Returns
ConstraintProperties

this

constrainMinWidth

@NonNull
public ConstraintProperties constrainMinWidth(@NonNull int width)

Sets the minimum width of the view. It is a dimension, It is only applicable if height is #MATCH_CONSTRAINT}.

Parameters
@NonNull int width

the minimum width of the view

Returns
ConstraintProperties

this

constrainWidth

@NonNull
public ConstraintProperties constrainWidth(@NonNull int width)

Sets the width of the view.

Parameters
@NonNull int width

the width of the view

Returns
ConstraintProperties

this

dimensionRatio

@NonNull
public ConstraintProperties dimensionRatio(@NonNull String ratio)

Constrains the views aspect ratio. For Example a HD screen is 16 by 9 = 16/(float)9 = 1.777f.

Parameters
@NonNull String ratio

The ratio of the width to height (width / height)

Returns
ConstraintProperties

this

elevation

@NonNull
public ConstraintProperties elevation(@NonNull float elevation)

Set the elevation of a view.

the elevation

Returns
ConstraintProperties

this

goneMargin

@NonNull
public ConstraintProperties goneMargin(@NonNull int anchor, @NonNull int value)

Sets the gone margin.

Parameters
@NonNull int anchor

The side to adjust the margin on

@NonNull int value

The new value for the margin

Returns
ConstraintProperties

this

horizontalBias

@NonNull
public ConstraintProperties horizontalBias(@NonNull float bias)

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

Parameters
@NonNull float bias

the new bias 0.5 is in the middle

Returns
ConstraintProperties

this

horizontalChainStyle

@NonNull
public ConstraintProperties horizontalChainStyle(@NonNull int chainStyle)

How the elements of the horizontal chain will be positioned. If the dimension behaviour is set to MATCH_CONSTRAINT. The possible values are:

  • CHAIN_SPREAD -- the elements will be spread out
  • 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 bias attribute of the child will then affect the positioning of the packed elements
Parameters
@NonNull int chainStyle

the weight that we can use to distribute the horizontal space

Returns
ConstraintProperties

this

horizontalWeight

@NonNull
public ConstraintProperties horizontalWeight(@NonNull 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

Parameters
@NonNull float weight

the weight that we can use to distribute the horizontal space

Returns
ConstraintProperties

this

margin

@NonNull
public ConstraintProperties margin(@NonNull int anchor, @NonNull int value)

Sets the margin.

Parameters
@NonNull int anchor

The side to adjust the margin on

@NonNull int value

The new value for the margin

Returns
ConstraintProperties

this

removeConstraints

@NonNull
public ConstraintProperties removeConstraints(@NonNull int anchor)

Remove a constraint from this view.

Parameters
@NonNull int anchor

the Anchor to remove constraint from

Returns
ConstraintProperties

this

removeFromHorizontalChain

@NonNull
public ConstraintProperties removeFromHorizontalChain()

Removes a view from a vertical chain. This assumes the view is connected to a vertical chain. Its behaviour is undefined if not part of a vertical chain.

Returns
ConstraintProperties

this

removeFromVerticalChain

@NonNull
public ConstraintProperties removeFromVerticalChain()

Removes a view from a vertical chain. This assumes the view is connected to a vertical chain. Its behaviour is undefined if not part of a vertical chain.

Returns
ConstraintProperties

this

rotation

@NonNull
public ConstraintProperties rotation(@NonNull float rotation)

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

the rotation about the Z axis

Returns
ConstraintProperties

this

rotationX

@NonNull
public ConstraintProperties rotationX(@NonNull float rotationX)

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

the rotation about the X axis

Returns
ConstraintProperties

this

rotationY

@NonNull
public ConstraintProperties rotationY(@NonNull float rotationY)

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

the rotation about the Y axis

Returns
ConstraintProperties

this

scaleX

@NonNull
public ConstraintProperties scaleX(@NonNull float scaleX)

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

the scale in X

Returns
ConstraintProperties

this

scaleY

@NonNull
public ConstraintProperties scaleY(@NonNull float scaleY)

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

the scale in Y

Returns
ConstraintProperties

this

transformPivot

@NonNull
public ConstraintProperties transformPivot(
    @NonNull float transformPivotX,
    @NonNull float transformPivotY
)

Set X and Y location of the pivot point around which the view will rotate and scale.

Parameters
@NonNull float transformPivotX

X location of the pivot point.

@NonNull float transformPivotY

Y location of the pivot point.

Returns
ConstraintProperties

this

transformPivotX

@NonNull
public ConstraintProperties transformPivotX(@NonNull float transformPivotX)

Set X location of the pivot point around which the view will rotate and scale.

X location of the pivot point.

Returns
ConstraintProperties

this

transformPivotY

@NonNull
public ConstraintProperties transformPivotY(@NonNull float transformPivotY)

Set Y location of the pivot point around which the view will rotate and scale.

Y location of the pivot point.

Returns
ConstraintProperties

this

translation

@NonNull
public ConstraintProperties translation(@NonNull float translationX, @NonNull float translationY)

Adjust the post-layout X and Y translation of a view.

Parameters
@NonNull float translationX

the translation in X

@NonNull float translationY

the translation in Y

Returns
ConstraintProperties

this

translationX

@NonNull
public ConstraintProperties translationX(@NonNull float translationX)

Adjust the post-layout X translation of a view.

the translation in X

Returns
ConstraintProperties

this

translationY

@NonNull
public ConstraintProperties translationY(@NonNull float translationY)

Adjust the post-layout Y translation of a view.

the translation in Y

Returns
ConstraintProperties

this

translationZ

@NonNull
public ConstraintProperties translationZ(@NonNull float translationZ)

Adjust the post-layout translation in Z of a view. This is the preferred way to adjust the shadow.

the translationZ

Returns
ConstraintProperties

this

verticalBias

@NonNull
public ConstraintProperties verticalBias(@NonNull float bias)

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

Parameters
@NonNull float bias

the new bias 0.5 is in the middle

Returns
ConstraintProperties

this

verticalChainStyle

@NonNull
public ConstraintProperties verticalChainStyle(@NonNull int chainStyle)

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

  • CHAIN_SPREAD -- the elements will be spread out
  • 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 bias attribute of the child will then affect the positioning of the packed elements
Parameters
@NonNull int chainStyle

the weight that we can use to distribute the horizontal space

Returns
ConstraintProperties

this

verticalWeight

@NonNull
public ConstraintProperties verticalWeight(@NonNull 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

Parameters
@NonNull float weight

the weight that we can use to distribute the vertical space

Returns
ConstraintProperties

this

visibility

@NonNull
public ConstraintProperties visibility(@NonNull int visibility)

Adjust the visibility of a view.

the visibility (View.VISIBLE, View.INVISIBLE, View.GONE)

Returns
ConstraintProperties

this