Build apps faster with our new App builder! Check it out →

paddingFrom

Common

Modifier in Compose Foundation Layout

A [Modifier] that can add padding to position the content according to specified distances from its bounds to an [alignment line][AlignmentLine]. 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.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation-layout:1.8.0-alpha04")
}

Overloads

@Stable
fun Modifier.paddingFrom(
    alignmentLine: AlignmentLine,
    before: Dp = Dp.Unspecified,
    after: Dp = Dp.Unspecified
): Modifier

Parameters

namedescription
alignmentLinethe alignment line relative to which the padding is defined
beforethe distance between the container's top edge and the horizontal alignment line, or the container's start edge and the vertical alignment line
afterthe distance between the container's bottom edge and the horizontal alignment line, or the container's end edge and the vertical alignment line
@Stable
fun Modifier.paddingFrom(
    alignmentLine: AlignmentLine,
    before: TextUnit = TextUnit.Unspecified,
    after: TextUnit = TextUnit.Unspecified
): Modifier

Parameters

namedescription
alignmentLinethe alignment line relative to which the padding is defined
beforethe distance between the container's top edge and the horizontal alignment line, or the container's start edge and the vertical alignment line
afterthe distance between the container's bottom edge and the horizontal alignment line, or the container's end edge and the vertical alignment line

Code Example

PaddingFromSample

@Composable
fun PaddingFromSample() {
    // 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)
    )
}
by @alexstyl