{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

MeasurePolicy

public fun interface MeasurePolicy


Defines the measure and layout behavior of a Layout. Layout and MeasurePolicy are the way Compose layouts (such as Box, Column, etc.) are built, and they can also be used to achieve custom layouts.

See Layout samples for examples of how to use MeasurePolicy.

Intrinsic measurement methods define the intrinsic size of the layout. These can be queried by the layout's parent in order to obtain, in specific cases, more information about the size of the layout in the absence of specific constraints:

See also
Layout

Summary

Public methods

default int
maxIntrinsicHeight(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int width
)

The function used to calculate IntrinsicMeasurable.maxIntrinsicHeight.

default int
maxIntrinsicWidth(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int height
)

The function used to calculate IntrinsicMeasurable.maxIntrinsicWidth.

abstract @NonNull MeasureResult
measure(
    @NonNull MeasureScope receiver,
    @NonNull List<@NonNull Measurable> measurables,
    @NonNull Constraints constraints
)

The function that defines the measurement and layout.

default int
minIntrinsicHeight(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int width
)

The function used to calculate IntrinsicMeasurable.minIntrinsicHeight.

default int
minIntrinsicWidth(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int height
)

The function used to calculate IntrinsicMeasurable.minIntrinsicWidth.

Public methods

maxIntrinsicHeight

default int maxIntrinsicHeight(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int width
)

The function used to calculate IntrinsicMeasurable.maxIntrinsicHeight. It represents the minimum height such that increasing it further will not decrease the minimum intrinsic width.

maxIntrinsicWidth

default int maxIntrinsicWidth(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int height
)

The function used to calculate IntrinsicMeasurable.maxIntrinsicWidth. It represents the minimum width such that increasing it further will not decrease the minimum intrinsic height.

measure

abstract @NonNull MeasureResult measure(
    @NonNull MeasureScope receiver,
    @NonNull List<@NonNull Measurable> measurables,
    @NonNull Constraints constraints
)

The function that defines the measurement and layout. Each Measurable in the measurables list corresponds to a layout child of the layout, and children can be measured using the Measurable.measure method. This method takes the Constraints which the child should respect; different children can be measured with different constraints. Measuring a child returns a Placeable, which reveals the size chosen by the child as a result of its own measurement. According to the children sizes, the parent defines the position of the children, by placing the Placeables in the MeasureResult.placeChildren of the returned MeasureResult. Therefore the parent needs to measure its children with appropriate Constraints, such that whatever valid sizes children choose, they can be laid out correctly according to the parent's layout algorithm. This is because there is no measurement negotiation between the parent and children: once a child chooses its size, the parent needs to handle it correctly.

Note that a child is allowed to choose a size that does not satisfy its constraints. However, when this happens, the placeable's width and height will not represent the real size of the child, but rather the size coerced in the child's constraints. Therefore, it is common for parents to assume in their layout algorithm that its children will always respect the constraints. 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. Rarely, when a parent really needs to know the true size of the child, they can read this from the placeable's Placeable.measuredWidth and Placeable.measuredHeight.

MeasureResult objects are usually created using the MeasureScope.layout factory, which takes the calculated size of this layout, its alignment lines, and a block defining the positioning of the children layouts.

minIntrinsicHeight

default int minIntrinsicHeight(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int width
)

The function used to calculate IntrinsicMeasurable.minIntrinsicHeight. It represents the minimum height this layout can take, given a specific width, such that the content of the layout will be painted correctly.

minIntrinsicWidth

default int minIntrinsicWidth(
    @NonNull IntrinsicMeasureScope receiver,
    @NonNull List<@NonNull IntrinsicMeasurable> measurables,
    int height
)

The function used to calculate IntrinsicMeasurable.minIntrinsicWidth. It represents the minimum width this layout can take, given a specific height, such that the content of the layout can be painted correctly.