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

AlignmentLineKt

public final class AlignmentLineKt


Summary

Public methods

static final @NonNull Modifier
paddingFrom(
    @NonNull Modifier receiver,
    @NonNull AlignmentLine alignmentLine,
    @NonNull Dp before,
    @NonNull Dp after
)

A Modifier that can add padding to position the content according to specified distances from its bounds to an alignment line.

static final @NonNull Modifier
paddingFrom(
    @NonNull Modifier receiver,
    @NonNull AlignmentLine alignmentLine,
    @NonNull TextUnit before,
    @NonNull TextUnit after
)

A Modifier that can add padding to position the content according to specified distances from its bounds to an alignment line.

static final @NonNull Modifier
paddingFromBaseline(
    @NonNull Modifier receiver,
    @NonNull Dp top,
    @NonNull Dp bottom
)

A Modifier that positions the content in a layout such that the distance from the top of the layout to the baseline of the first line of text in the content is top, and the distance from the baseline of the last line of text in the content to the bottom of the layout is bottom.

static final @NonNull Modifier
paddingFromBaseline(
    @NonNull Modifier receiver,
    @NonNull TextUnit top,
    @NonNull TextUnit bottom
)

A Modifier that positions the content in a layout such that the distance from the top of the layout to the baseline of the first line of text in the content is top, and the distance from the baseline of the last line of text in the content to the bottom of the layout is bottom.

Public methods

paddingFrom

public static final @NonNull Modifier paddingFrom(
    @NonNull Modifier receiver,
    @NonNull AlignmentLine alignmentLine,
    @NonNull Dp before,
    @NonNull Dp after
)

A Modifier that can add padding to position the content according to specified distances from its bounds to an alignment line. Whether the positioning is vertical or horizontal is defined by the orientation of the given alignmentLine (if the line is horizontal, before and after will refer to distances from top and bottom, otherwise they will refer to distances from start and end). The opposite axis sizing and positioning will remain unaffected. The modified layout will try to include the required padding, subject to the incoming max layout constraints, such that the distance from its bounds to the alignmentLine of the content will be before and after, respectively. When the max constraints do not allow this, satisfying the before requirement will have priority over after. When the modified layout is min constrained in the affected layout direction and the padded layout is smaller than the constraint, the modified layout will satisfy the min constraint and the content will be positioned to satisfy the before requirement if specified, or the after requirement otherwise.

import androidx.compose.foundation.layout.paddingFrom
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

// We want to have 30.sp distance from the top of the layout box to the baseline of the
// first line of text.
val distanceToBaseline = 30.sp
// We convert the 30.sp value to dps, which is required for the paddingFrom API.
val distanceToBaselineDp = with(LocalDensity.current) { distanceToBaseline.toDp() }
// The result will be a layout with 30.sp distance from the top of the layout box to the
// baseline of the first line of text.
Text(
    text = "This is an example.",
    modifier = Modifier.paddingFrom(FirstBaseline, before = distanceToBaselineDp)
)
Parameters
@NonNull AlignmentLine alignmentLine

the alignment line relative to which the padding is defined

@NonNull Dp before

the distance between the container's top edge and the horizontal alignment line, or the container's start edge and the vertical alignment line

@NonNull Dp after

the distance between the container's bottom edge and the horizontal alignment line, or the container's end edge and the vertical alignment line

See also
paddingFromBaseline

Example usage:

paddingFrom

public static final @NonNull Modifier paddingFrom(
    @NonNull Modifier receiver,
    @NonNull AlignmentLine alignmentLine,
    @NonNull TextUnit before,
    @NonNull TextUnit after
)

A Modifier that can add padding to position the content according to specified distances from its bounds to an alignment line. Whether the positioning is vertical or horizontal is defined by the orientation of the given alignmentLine (if the line is horizontal, before and after will refer to distances from top and bottom, otherwise they will refer to distances from start and end). The opposite axis sizing and positioning will remain unaffected. The modified layout will try to include the required padding, subject to the incoming max layout constraints, such that the distance from its bounds to the alignmentLine of the content will be before and after, respectively. When the max constraints do not allow this, satisfying the before requirement will have priority over after. When the modified layout is min constrained in the affected layout direction and the padded layout is smaller than the constraint, the modified layout will satisfy the min constraint and the content will be positioned to satisfy the before requirement if specified, or the after requirement otherwise.

import androidx.compose.foundation.layout.paddingFrom
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

// We want to have 30.sp distance from the top of the layout box to the baseline of the
// first line of text.
val distanceToBaseline = 30.sp
// We convert the 30.sp value to dps, which is required for the paddingFrom API.
val distanceToBaselineDp = with(LocalDensity.current) { distanceToBaseline.toDp() }
// The result will be a layout with 30.sp distance from the top of the layout box to the
// baseline of the first line of text.
Text(
    text = "This is an example.",
    modifier = Modifier.paddingFrom(FirstBaseline, before = distanceToBaselineDp)
)
Parameters
@NonNull AlignmentLine alignmentLine

the alignment line relative to which the padding is defined

@NonNull TextUnit before

the distance between the container's top edge and the horizontal alignment line, or the container's start edge and the vertical alignment line

@NonNull TextUnit after

the distance between the container's bottom edge and the horizontal alignment line, or the container's end edge and the vertical alignment line

See also
paddingFromBaseline

Example usage:

paddingFromBaseline

public static final @NonNull Modifier paddingFromBaseline(
    @NonNull Modifier receiver,
    @NonNull Dp top,
    @NonNull Dp bottom
)

A Modifier that positions the content in a layout such that the distance from the top of the layout to the baseline of the first line of text in the content is top, and the distance from the baseline of the last line of text in the content to the bottom of the layout is bottom.

When the modified layout is min height constrained and the padded layout is smaller than the constraint, the modified layout will satisfy the min constraint and the content will be positioned to satisfy the top requirement if specified, or the bottom requirement otherwise.

import androidx.compose.foundation.layout.paddingFrom
import androidx.compose.foundation.layout.paddingFromBaseline
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

// We want to have 30.dp distance from the top of the layout box to the baseline of the
// first line of text, and a 40.dp distance from the bottom of the layout box to the baseline
// of the last line of text. Note it is good practice to specify these distances in sp for font
// scaling, which can be done with the other overload.
val distanceToFirstBaseline = 30.dp
val distanceFromLastBaseline = 40.dp
Text(
    text = "This line has the first baseline.\nThis line has the last baseline.",
    modifier = Modifier.paddingFromBaseline(distanceToFirstBaseline, distanceFromLastBaseline)
)
See also
paddingFrom

Example usage:

paddingFromBaseline

public static final @NonNull Modifier paddingFromBaseline(
    @NonNull Modifier receiver,
    @NonNull TextUnit top,
    @NonNull TextUnit bottom
)

A Modifier that positions the content in a layout such that the distance from the top of the layout to the baseline of the first line of text in the content is top, and the distance from the baseline of the last line of text in the content to the bottom of the layout is bottom.

When the modified layout is min height constrained and the padded layout is smaller than the constraint, the modified layout will satisfy the min constraint and the content will be positioned to satisfy the top requirement if specified, or the bottom requirement otherwise.

import androidx.compose.foundation.layout.paddingFrom
import androidx.compose.foundation.layout.paddingFromBaseline
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.unit.sp

// We want to have 30.sp distance from the top of the layout box to the baseline of the
// first line of text, and a 40.sp distance from the bottom of the layout box to the baseline
// of the last line of text.
val distanceToFirstBaseline = 30.sp
val distanceFromLastBaseline = 40.sp
Text(
    text = "This line has the first baseline.\nThis line has the last baseline.",
    modifier = Modifier.paddingFromBaseline(distanceToFirstBaseline, distanceFromLastBaseline)
)
See also
paddingFrom

Example usage: