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

Dp

value public final class Dp implements Comparable


Dimension value representing device-independent pixels (dp). Component APIs specify their dimensions such as line thickness in DP with Dp objects. Hairline (1 pixel) thickness may be specified with Hairline, a dimension that take up no space. Dp are normally defined using dp, which can be applied to Int, Double, and Float.

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

Box(
    Modifier.padding(
        10.dp, // Int
        10f.dp, // Float
        20.0.dp, // Double
        10.dp
    )
)

Drawing and Layout are done in pixels. To retrieve the pixel size of a Dp, use Density.toPx:

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.unit.dp

val lineThickness = 6.dp
Canvas(Modifier.fillMaxSize()) {
    val lineThicknessPx = lineThickness.toPx()
    inset(lineThicknessPx / 2) {
        drawRect(Color.Red, style = Stroke(lineThicknessPx))
    }
}

Summary

Nested types

public static class Dp.Companion

Public constructors

Dp(float value)

Public methods

int

Support comparing Dimensions with comparison operators.

final float
div(@NonNull Dp other)

Divide by another Dp to get a scalar.

final @NonNull Dp
div(float other)

Divide a Dp by a scalar.

final @NonNull Dp
div(int other)
final float
final @NonNull Dp
minus(@NonNull Dp other)

Subtract a Dp from another one.

final @NonNull Dp
plus(@NonNull Dp other)

Add two Dps together.

final @NonNull Dp
times(float other)

Multiply a Dp by a scalar.

final @NonNull Dp
times(int other)
@NonNull String
final @NonNull Dp

This is the same as multiplying the Dp by -1.0.

Extension functions

final void
BoundsAssertionsKt.assertIsEqualTo(
    @NonNull Dp receiver,
    @NonNull Dp expected,
    @NonNull String subject,
    @NonNull Dp tolerance
)

Asserts that this value is equal to the given expected value.

final @NonNull Dp
DpKt.coerceAtLeast(@NonNull Dp receiver, @NonNull Dp minimumValue)

Ensures that this value is not less than the specified minimumValue.

final @NonNull Dp
DpKt.coerceAtMost(@NonNull Dp receiver, @NonNull Dp maximumValue)

Ensures that this value is not greater than the specified maximumValue.

final @NonNull Dp
DpKt.coerceIn(
    @NonNull Dp receiver,
    @NonNull Dp minimumValue,
    @NonNull Dp maximumValue
)

Ensures that this value lies in the specified range minimumValue..

final boolean
DpKt.isFinite(@NonNull Dp receiver)

Return true when it is finite or false when it is Dp.Infinity

final boolean

false when this is Dp.Unspecified.

final boolean

true when this is Dp.Unspecified.

final @NonNull Dp
DpKt.takeOrElse(@NonNull Dp receiver, @NonNull Function0<@NonNull Dp> block)

If this Dp then this is returned, otherwise block is executed and its result is returned.

Public constructors

Dp

public Dp(float value)

Public methods

compareTo

public int compareTo(@NonNull Dp other)

Support comparing Dimensions with comparison operators.

div

public final float div(@NonNull Dp other)

Divide by another Dp to get a scalar.

div

public final @NonNull Dp div(float other)

Divide a Dp by a scalar.

div

public final @NonNull Dp div(int other)

getValue

public final float getValue()

minus

public final @NonNull Dp minus(@NonNull Dp other)

Subtract a Dp from another one.

plus

public final @NonNull Dp plus(@NonNull Dp other)

Add two Dps together.

times

public final @NonNull Dp times(float other)

Multiply a Dp by a scalar.

times

public final @NonNull Dp times(int other)

toString

public @NonNull String toString()

unaryMinus

public final @NonNull Dp unaryMinus()

This is the same as multiplying the Dp by -1.0.

Extension functions

BoundsAssertionsKt.assertIsEqualTo

public final void BoundsAssertionsKt.assertIsEqualTo(
    @NonNull Dp receiver,
    @NonNull Dp expected,
    @NonNull String subject,
    @NonNull Dp tolerance
)

Asserts that this value is equal to the given expected value.

Performs the comparison with the given tolerance or the default one if none is provided. It is recommended to use tolerance when comparing positions and size coming from the framework as there can be rounding operation performed by individual layouts so the values can be slightly off from the expected ones.

Parameters
@NonNull Dp expected

The expected value to which this one should be equal to.

@NonNull String subject

Used in the error message to identify which item this assertion failed on.

@NonNull Dp tolerance

The tolerance within which the values should be treated as equal.

Throws
kotlin.AssertionError

if comparison fails.

DpKt.coerceAtLeast

public final @NonNull Dp DpKt.coerceAtLeast(@NonNull Dp receiver, @NonNull Dp minimumValue)

Ensures that this value is not less than the specified minimumValue.

Returns
@NonNull Dp

this value if it's greater than or equal to the minimumValue or the minimumValue otherwise.

DpKt.coerceAtMost

public final @NonNull Dp DpKt.coerceAtMost(@NonNull Dp receiver, @NonNull Dp maximumValue)

Ensures that this value is not greater than the specified maximumValue.

Returns
@NonNull Dp

this value if it's less than or equal to the maximumValue or the maximumValue otherwise.

DpKt.coerceIn

public final @NonNull Dp DpKt.coerceIn(
    @NonNull Dp receiver,
    @NonNull Dp minimumValue,
    @NonNull Dp maximumValue
)

Ensures that this value lies in the specified range minimumValue..maximumValue.

Returns
@NonNull Dp

this value if it's in the range, or minimumValue if this value is less than minimumValue, or maximumValue if this value is greater than maximumValue.

DpKt.isFinite

public final boolean DpKt.isFinite(@NonNull Dp receiver)

Return true when it is finite or false when it is Dp.Infinity

DpKt.isSpecified

public final boolean DpKt.isSpecified(@NonNull Dp receiver)

false when this is Dp.Unspecified.

DpKt.isUnspecified

public final boolean DpKt.isUnspecified(@NonNull Dp receiver)

true when this is Dp.Unspecified.

DpKt.takeOrElse

public final @NonNull Dp DpKt.takeOrElse(@NonNull Dp receiver, @NonNull Function0<@NonNull Dp> block)

If this Dp then this is returned, otherwise block is executed and its result is returned.