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 int

The baseline of the text in a view.

static final int
BOTTOM = 4

The bottom side of a view.

static final int
END = 7

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

static final int
LEFT = 1

The left side of a view.

static final int

Dimension will be controlled by constraints

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
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

Dimension will set by the view's content

Public constructors

Public methods

ConstraintProperties
addToHorizontalChain(int leftId, int rightId)

Adds the view to a horizontal chain.

ConstraintProperties
addToHorizontalChainRTL(int leftId, int rightId)

Adds the view to a horizontal chain using RTL attributes.

ConstraintProperties
addToVerticalChain(int topId, int bottomId)

Adds a view to a vertical chain.

ConstraintProperties
alpha(float alpha)

Adjust the alpha of a view.

void

Should be called to apply the changes currently a no op in place for subclasses and future use

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

Center view between the other two widgets.

ConstraintProperties
centerHorizontally(int toView)

Centers the view horizontally relative to toView's position.

ConstraintProperties
centerHorizontally(
    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.

ConstraintProperties

Centers the view horizontally relative to toView's position.

ConstraintProperties
centerHorizontallyRtl(
    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.

ConstraintProperties
centerVertically(int toView)

Centers the view vertically relative to toView's position.

ConstraintProperties
centerVertically(
    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.

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

Create a constraint between two widgets.

ConstraintProperties

Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

ConstraintProperties

Sets how the width is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD.

ConstraintProperties
constrainHeight(int height)

Sets the height of the view.

ConstraintProperties
constrainMaxHeight(int height)

Sets the maximum height of the view.

ConstraintProperties
constrainMaxWidth(int width)

Sets the maximum width of the view.

ConstraintProperties
constrainMinHeight(int height)

Sets the minimum height of the view.

ConstraintProperties
constrainMinWidth(int width)

Sets the minimum width of the view.

ConstraintProperties
constrainWidth(int width)

Sets the width of the view.

ConstraintProperties

Constrains the views aspect ratio.

ConstraintProperties
elevation(float elevation)

Set the elevation of a view.

ConstraintProperties
goneMargin(int anchor, int value)

Sets the gone margin.

ConstraintProperties
horizontalBias(float bias)

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

ConstraintProperties
horizontalChainStyle(int chainStyle)

How the elements of the horizontal chain will be positioned.

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

ConstraintProperties
margin(int anchor, int value)

Sets the margin.

ConstraintProperties
removeConstraints(int anchor)

Remove a constraint from this view.

ConstraintProperties

Removes a view from a vertical chain.

ConstraintProperties

Removes a view from a vertical chain.

ConstraintProperties
rotation(float rotation)

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

ConstraintProperties
rotationX(float rotationX)

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

ConstraintProperties
rotationY(float rotationY)

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

ConstraintProperties
scaleX(float scaleX)

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

ConstraintProperties
scaleY(float scaleY)

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

ConstraintProperties
transformPivot(float transformPivotX, float transformPivotY)

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

ConstraintProperties
transformPivotX(float transformPivotX)

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

ConstraintProperties
transformPivotY(float transformPivotY)

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

ConstraintProperties
translation(float translationX, float translationY)

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

ConstraintProperties
translationX(float translationX)

Adjust the post-layout X translation of a view.

ConstraintProperties
translationY(float translationY)

Adjust the post-layout Y translation of a view.

ConstraintProperties
translationZ(float translationZ)

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

ConstraintProperties
verticalBias(float bias)

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

ConstraintProperties
verticalChainStyle(int chainStyle)

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

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

ConstraintProperties
visibility(int visibility)

Adjust the visibility of a view.

Constants

BASELINE

Added in 2.2.0-alpha13
public static final int BASELINE = 5

The baseline of the text in a view.

BOTTOM

Added in 2.2.0-alpha13
public static final int BOTTOM = 4

The bottom side of a view.

END

Added in 2.2.0-alpha13
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

LEFT

Added in 2.2.0-alpha13
public static final int LEFT = 1

The left side of a view.

MATCH_CONSTRAINT

Added in 2.2.0-alpha13
public static final int MATCH_CONSTRAINT = 0

Dimension will be controlled by constraints

MATCH_CONSTRAINT_SPREAD

Added in 2.2.0-alpha13
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.0-alpha13
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.0-alpha13
public static final int PARENT_ID = 0

References the id of the parent.

RIGHT

Added in 2.2.0-alpha13
public static final int RIGHT = 2

The right side of a view.

START

Added in 2.2.0-alpha13
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.0-alpha13
public static final int TOP = 3

The top of a view.

UNSET

Added in 2.2.0-alpha13
public static final int UNSET = -1

Used to indicate a parameter is cleared or not set

WRAP_CONTENT

Added in 2.2.0-alpha13
public static final int WRAP_CONTENT = -2

Dimension will set by the view's content

Public constructors

ConstraintProperties

Added in 2.2.0-alpha13
public ConstraintProperties(View view)

Public methods

addToHorizontalChain

Added in 2.2.0-alpha13
public ConstraintProperties addToHorizontalChain(int leftId, int rightId)

Adds the view to a horizontal chain.

Parameters
int leftId

id of the view in chain to the left

int rightId

id of the view in chain to the right

Returns
ConstraintProperties

this

addToHorizontalChainRTL

Added in 2.2.0-alpha13
public ConstraintProperties addToHorizontalChainRTL(int leftId, int rightId)

Adds the view to a horizontal chain using RTL attributes.

Parameters
int leftId

id of the view in chain to the left

int rightId

id of the view in chain to the right

Returns
ConstraintProperties

this

addToVerticalChain

Added in 2.2.0-alpha13
public ConstraintProperties addToVerticalChain(int topId, int bottomId)

Adds a view to a vertical chain.

Parameters
int topId

view above.

int bottomId

view below

Returns
ConstraintProperties

this

alpha

Added in 2.2.0-alpha13
public ConstraintProperties alpha(float alpha)

Adjust the alpha of a view.

Parameters
float alpha

the alpha

Returns
ConstraintProperties

this

apply

Added in 2.2.0-alpha13
public void apply()

Should be called to apply the changes currently a no op in place for subclasses and future use

center

Added in 2.2.0-alpha13
public ConstraintProperties center(
    int firstID,
    int firstSide,
    int firstMargin,
    int secondId,
    int secondSide,
    int secondMargin,
    float bias
)

Center view between the other two widgets.

Parameters
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

Returns
ConstraintProperties

this

centerHorizontally

Added in 2.2.0-alpha13
public ConstraintProperties centerHorizontally(int toView)

Centers the view horizontally relative to toView's position.

Parameters
int toView

ID of view to center on (or in)

Returns
ConstraintProperties

this

centerHorizontally

Added in 2.2.0-alpha13
public ConstraintProperties centerHorizontally(
    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.

Parameters
int leftId

The Id of the widget on the left side

int leftSide

The side of the leftId widget to connect to

int leftMargin

The margin on the left side

int rightId

The Id of the widget on the right side

int rightSide

The side of the rightId widget to connect to

int rightMargin

The margin on the right side

float bias

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

Returns
ConstraintProperties

this

centerHorizontallyRtl

Added in 2.2.0-alpha13
public ConstraintProperties centerHorizontallyRtl(int toView)

Centers the view horizontally relative to toView's position.

Parameters
int toView

ID of view to center on (or in)

Returns
ConstraintProperties

this

centerHorizontallyRtl

Added in 2.2.0-alpha13
public ConstraintProperties centerHorizontallyRtl(
    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.

Parameters
int startId

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

int startSide

The side of the startId widget to connect to

int startMargin

The margin on the start side

int endId

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

int endSide

The side of the endId widget to connect to

int endMargin

The margin on the end side

float bias

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

Returns
ConstraintProperties

this

centerVertically

Added in 2.2.0-alpha13
public ConstraintProperties centerVertically(int toView)

Centers the view vertically relative to toView's position.

Parameters
int toView

ID of view to center on (or in)

Returns
ConstraintProperties

this

centerVertically

Added in 2.2.0-alpha13
public ConstraintProperties centerVertically(
    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.

Parameters
int topId

The Id of the widget on the top side

int topSide

The side of the leftId widget to connect to

int topMargin

The margin on the top side

int bottomId

The Id of the widget on the bottom side

int bottomSide

The side of the bottomId widget to connect to

int bottomMargin

The margin on the bottom side

float bias

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

Returns
ConstraintProperties

this

connect

Added in 2.2.0-alpha13
public ConstraintProperties connect(int startSide, int endID, int endSide, int margin)

Create a constraint between two widgets.

Parameters
int startSide

the side of the widget to constrain

int endID

the id of the widget to constrain to

int endSide

the side of widget to constrain to

int margin

the margin to constrain (margin must be positive)

constrainDefaultHeight

Added in 2.2.0-alpha13
public ConstraintProperties constrainDefaultHeight(int height)

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

Parameters
int height

MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD

Returns
ConstraintProperties

this

constrainDefaultWidth

Added in 2.2.0-alpha13
public ConstraintProperties constrainDefaultWidth(int width)

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

Parameters
int width

MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD

Returns
ConstraintProperties

this

constrainHeight

Added in 2.2.0-alpha13
public ConstraintProperties constrainHeight(int height)

Sets the height of the view.

Parameters
int height

the height of the view

Returns
ConstraintProperties

this

constrainMaxHeight

Added in 2.2.0-alpha13
public ConstraintProperties constrainMaxHeight(int height)

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

Parameters
int height

the maximum height of the view

Returns
ConstraintProperties

this

constrainMaxWidth

Added in 2.2.0-alpha13
public ConstraintProperties constrainMaxWidth(int width)

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

Parameters
int width

the maximum width of the view

Returns
ConstraintProperties

this

constrainMinHeight

Added in 2.2.0-alpha13
public ConstraintProperties constrainMinHeight(int height)

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

Parameters
int height

the minimum height of the view

Returns
ConstraintProperties

this

constrainMinWidth

Added in 2.2.0-alpha13
public ConstraintProperties constrainMinWidth(int width)

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

Parameters
int width

the minimum width of the view

Returns
ConstraintProperties

this

constrainWidth

Added in 2.2.0-alpha13
public ConstraintProperties constrainWidth(int width)

Sets the width of the view.

Parameters
int width

the width of the view

Returns
ConstraintProperties

this

dimensionRatio

Added in 2.2.0-alpha13
public ConstraintProperties dimensionRatio(String ratio)

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

Parameters
String ratio

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

Returns
ConstraintProperties

this

elevation

Added in 2.2.0-alpha13
public ConstraintProperties elevation(float elevation)

Set the elevation of a view.

Parameters
float elevation

the elevation

Returns
ConstraintProperties

this

goneMargin

Added in 2.2.0-alpha13
public ConstraintProperties goneMargin(int anchor, int value)

Sets the gone margin.

Parameters
int anchor

The side to adjust the margin on

int value

The new value for the margin

Returns
ConstraintProperties

this

horizontalBias

Added in 2.2.0-alpha13
public ConstraintProperties horizontalBias(float bias)

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

Parameters
float bias

the new bias 0.5 is in the middle

Returns
ConstraintProperties

this

horizontalChainStyle

Added in 2.2.0-alpha13
public ConstraintProperties horizontalChainStyle(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
int chainStyle

the weight that we can use to distribute the horizontal space

Returns
ConstraintProperties

this

horizontalWeight

Added in 2.2.0-alpha13
public ConstraintProperties horizontalWeight(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
float weight

the weight that we can use to distribute the horizontal space

Returns
ConstraintProperties

this

margin

Added in 2.2.0-alpha13
public ConstraintProperties margin(int anchor, int value)

Sets the margin.

Parameters
int anchor

The side to adjust the margin on

int value

The new value for the margin

Returns
ConstraintProperties

this

removeConstraints

Added in 2.2.0-alpha13
public ConstraintProperties removeConstraints(int anchor)

Remove a constraint from this view.

Parameters
int anchor

the Anchor to remove constraint from

Returns
ConstraintProperties

this

removeFromHorizontalChain

Added in 2.2.0-alpha13
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

Added in 2.2.0-alpha13
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

Added in 2.2.0-alpha13
public ConstraintProperties rotation(float rotation)

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

Parameters
float rotation

the rotation about the Z axis

Returns
ConstraintProperties

this

rotationX

Added in 2.2.0-alpha13
public ConstraintProperties rotationX(float rotationX)

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

Parameters
float rotationX

the rotation about the X axis

Returns
ConstraintProperties

this

rotationY

Added in 2.2.0-alpha13
public ConstraintProperties rotationY(float rotationY)

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

Parameters
float rotationY

the rotation about the Y axis

Returns
ConstraintProperties

this

scaleX

Added in 2.2.0-alpha13
public ConstraintProperties scaleX(float scaleX)

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

Parameters
float scaleX

the scale in X

Returns
ConstraintProperties

this

scaleY

Added in 2.2.0-alpha13
public ConstraintProperties scaleY(float scaleY)

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

Parameters
float scaleY

the scale in Y

Returns
ConstraintProperties

this

transformPivot

Added in 2.2.0-alpha13
public ConstraintProperties transformPivot(float transformPivotX, float transformPivotY)

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

Parameters
float transformPivotX

X location of the pivot point.

float transformPivotY

Y location of the pivot point.

Returns
ConstraintProperties

this

transformPivotX

Added in 2.2.0-alpha13
public ConstraintProperties transformPivotX(float transformPivotX)

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

Parameters
float transformPivotX

X location of the pivot point.

Returns
ConstraintProperties

this

transformPivotY

Added in 2.2.0-alpha13
public ConstraintProperties transformPivotY(float transformPivotY)

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

Parameters
float transformPivotY

Y location of the pivot point.

Returns
ConstraintProperties

this

translation

Added in 2.2.0-alpha13
public ConstraintProperties translation(float translationX, float translationY)

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

Parameters
float translationX

the translation in X

float translationY

the translation in Y

Returns
ConstraintProperties

this

translationX

Added in 2.2.0-alpha13
public ConstraintProperties translationX(float translationX)

Adjust the post-layout X translation of a view.

Parameters
float translationX

the translation in X

Returns
ConstraintProperties

this

translationY

Added in 2.2.0-alpha13
public ConstraintProperties translationY(float translationY)

Adjust the post-layout Y translation of a view.

Parameters
float translationY

the translation in Y

Returns
ConstraintProperties

this

translationZ

Added in 2.2.0-alpha13
public ConstraintProperties translationZ(float translationZ)

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

Parameters
float translationZ

the translationZ

Returns
ConstraintProperties

this

verticalBias

Added in 2.2.0-alpha13
public ConstraintProperties verticalBias(float bias)

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

Parameters
float bias

the new bias 0.5 is in the middle

Returns
ConstraintProperties

this

verticalChainStyle

Added in 2.2.0-alpha13
public ConstraintProperties verticalChainStyle(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
int chainStyle

the weight that we can use to distribute the horizontal space

Returns
ConstraintProperties

this

verticalWeight

Added in 2.2.0-alpha13
public ConstraintProperties verticalWeight(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
float weight

the weight that we can use to distribute the vertical space

Returns
ConstraintProperties

this

visibility

Added in 2.2.0-alpha13
public ConstraintProperties visibility(int visibility)

Adjust the visibility of a view.

Parameters
int visibility

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

Returns
ConstraintProperties

this