Google is committed to advancing racial equity for Black communities. See how.

Constraints2

@Immutable inline class Constraints2
kotlin.Any
   ↳ androidx.ui.core.Constraints2

Immutable constraints used for measuring child Layouts or LayoutModifiers. A parent layout can measure their children using the measure method on the corresponding Measurables, method which takes the Constraints2 the child has to follow. A measured child is then responsible to choose for themselves and return a size which satisfies the set of Constraints2 received from their parent:

  • minWidth <= chosenWidth <= maxWidth
  • minHeight <= chosenHeight <= maxHeight The parent can then access the child chosen size on the resulting Placeable. The parent is responsible of defining a valid positioning of the children according to their sizes, so the parent needs to measure the children with appropriate Constraints2, such that whatever valid sizes children choose, they can be laid out in a way that also respects the parent's incoming Constraints2. Note that different children can be measured with different Constraints2. A child is allowed to choose a size that does not satisfy its constraints. However, when this happens, the parent will not read from the Placeable the real size of the child, but rather one that was coerced in the child's constraints; therefore, a parent can assume that its children will always respect the constraints in their layout algorithm. When this does not happen in reality, the position assigned to the child will be automatically offset to be centered on the space assigned by the parent under the assumption that constraints were respected. A set of Constraints2 can have infinite maxWidth and/or maxHeight. This is a trick often used by parents to ask their children for their preferred size: unbounded constraints force children whose default behavior is to fill the available space (always size to maxWidth/maxHeight) to have an opinion about their preferred size. Most commonly, when measured with unbounded Constraints2, these children will fallback to size themselves to wrap their content, instead of expanding to fill the available space (this is not always true as it depends on the child layout model, but is a common behavior for core layout components).

Constraints2 uses a Long to represent four values, minWidth, minHeight, maxWidth, and maxHeight. The range of the values varies to allow for at most 256K in one dimension. There are four possible maximum ranges, 13 bits/18 bits, and 15 bits/16 bits for either width or height, depending on the needs. For example, a width could range up to 18 bits and the height up to 13 bits. Alternatively, the width could range up to 16 bits and the height up to 15 bits. The height and width requirements can be reversed, with a height of up to 18 bits and width of 13 bits or height of 16 bits and width of 15 bits. Any constraints exceeding this range will fail.

Summary

Constants

const Int

A value that maxWidth or maxHeight will be set to when the constraint should be considered infinite.

Public constructors

<init>(value: Long)

Immutable constraints used for measuring child Layouts or LayoutModifiers.

Public methods

Constraints2
copy(minWidth: Int = this.minWidth, maxWidth: Int = this.maxWidth, minHeight: Int = this.minHeight, maxHeight: Int = this.maxHeight)

Copies the existing Constraints2, replacing some of minWidth, minHeight, maxWidth, or maxHeight as desired.

Companion functions

Constraints2
fixed(width: Int, height: Int)

Creates constraints for fixed size in both dimensions.

Constraints2
fixedHeight(height: Int)

Creates constraints for fixed height and unspecified width.

Constraints2
fixedWidth(width: Int)

Creates constraints for fixed width and unspecified height.

Extension functions

From androidx.ui.core
IntSize

Takes a size and returns the closest size to it that satisfies the constraints.

Constraints2
Constraints2.enforce(otherConstraints: Constraints2)

Returns the result of coercing the current constraints in a different set of constraints.

Constraints2
Constraints2.offset(horizontal: Int = 0, vertical: Int = 0)

Returns the Constraints obtained by offsetting the current instance with the given values.

Boolean

Takes a size and returns whether it satisfies the current constraints.

Properties

Boolean

Whether or not the upper bound on the maximum height.

Boolean

Whether or not the upper bound on the maximum width.

Int

The maximum height that the measurement can take.

Int

The maximum width that the measurement can take.

Int

The minimum height that the measurement can take.

Int

The minimum width that the measurement can take.

Extension properties

From androidx.ui.core
Boolean

Whether there is exactly one height value that satisfies the constraints.

Boolean

Whether there is exactly one width value that satisfies the constraints.

Boolean

Whether the area of a component respecting these constraints will definitely be 0.

Constants

Infinity

const val Infinity: Int

A value that maxWidth or maxHeight will be set to when the constraint should be considered infinite. hasBoundedHeight or hasBoundedWidth will be true when maxHeight or maxWidth is Infinity, respectively.

Value: Int.MIN_VALUE / 2

Public constructors

<init>

Constraints2(value: Long)

Immutable constraints used for measuring child Layouts or LayoutModifiers. A parent layout can measure their children using the measure method on the corresponding Measurables, method which takes the Constraints2 the child has to follow. A measured child is then responsible to choose for themselves and return a size which satisfies the set of Constraints2 received from their parent:

  • minWidth <= chosenWidth <= maxWidth
  • minHeight <= chosenHeight <= maxHeight The parent can then access the child chosen size on the resulting Placeable. The parent is responsible of defining a valid positioning of the children according to their sizes, so the parent needs to measure the children with appropriate Constraints2, such that whatever valid sizes children choose, they can be laid out in a way that also respects the parent's incoming Constraints2. Note that different children can be measured with different Constraints2. A child is allowed to choose a size that does not satisfy its constraints. However, when this happens, the parent will not read from the Placeable the real size of the child, but rather one that was coerced in the child's constraints; therefore, a parent can assume that its children will always respect the constraints in their layout algorithm. When this does not happen in reality, the position assigned to the child will be automatically offset to be centered on the space assigned by the parent under the assumption that constraints were respected. A set of Constraints2 can have infinite maxWidth and/or maxHeight. This is a trick often used by parents to ask their children for their preferred size: unbounded constraints force children whose default behavior is to fill the available space (always size to maxWidth/maxHeight) to have an opinion about their preferred size. Most commonly, when measured with unbounded Constraints2, these children will fallback to size themselves to wrap their content, instead of expanding to fill the available space (this is not always true as it depends on the child layout model, but is a common behavior for core layout components).

Constraints2 uses a Long to represent four values, minWidth, minHeight, maxWidth, and maxHeight. The range of the values varies to allow for at most 256K in one dimension. There are four possible maximum ranges, 13 bits/18 bits, and 15 bits/16 bits for either width or height, depending on the needs. For example, a width could range up to 18 bits and the height up to 13 bits. Alternatively, the width could range up to 16 bits and the height up to 15 bits. The height and width requirements can be reversed, with a height of up to 18 bits and width of 13 bits or height of 16 bits and width of 15 bits. Any constraints exceeding this range will fail.

Public methods

copy

fun copy(
    minWidth: Int = this.minWidth,
    maxWidth: Int = this.maxWidth,
    minHeight: Int = this.minHeight,
    maxHeight: Int = this.maxHeight
): Constraints2

Copies the existing Constraints2, replacing some of minWidth, minHeight, maxWidth, or maxHeight as desired. minWidth and minHeight must be positive and maxWidth and maxHeight must be greater than or equal to minWidth and minHeight, respectively, or Infinity.

Properties

hasBoundedHeight

val hasBoundedHeight: Boolean

Whether or not the upper bound on the maximum height.

See Also

hasBoundedWidth

val hasBoundedWidth: Boolean

Whether or not the upper bound on the maximum width.

See Also

maxHeight

val maxHeight: Int

The maximum height that the measurement can take. This will either be a positive value greater than or equal to minHeight or Constraints2.Infinity.

maxWidth

val maxWidth: Int

The maximum width that the measurement can take. This will either be a positive value greater than or equal to minWidth or Constraints2.Infinity.

minHeight

val minHeight: Int

The minimum height that the measurement can take.

minWidth

val minWidth: Int

The minimum width that the measurement can take.

Companion functions

fixed

fun fixed(
    width: Int,
    height: Int
): Constraints2

Creates constraints for fixed size in both dimensions.

fixedHeight

fun fixedHeight(height: Int): Constraints2

Creates constraints for fixed height and unspecified width.

fixedWidth

fun fixedWidth(width: Int): Constraints2

Creates constraints for fixed width and unspecified height.