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 companion properties

Dp

A dimension used to represent a hairline drawing element.

Cmn
Dp

Infinite dp dimension.

Cmn
Dp

Constant that means unspecified Dp

Cmn

Public constructors

Dp(value: Float)
Cmn

Public functions

open operator Int
compareTo(other: Dp)

Support comparing Dimensions with comparison operators.

Cmn
inline operator Float
div(other: Dp)

Divide by another Dp to get a scalar.

Cmn
inline operator Dp
div(other: Float)

Divide a Dp by a scalar.

Cmn
inline operator Dp
div(other: Int)
Cmn
inline operator Dp
minus(other: Dp)

Subtract a Dp from another one.

Cmn
inline operator Dp
plus(other: Dp)

Add two Dps together.

Cmn
inline operator Dp
times(other: Float)

Multiply a Dp by a scalar.

Cmn
inline operator Dp
times(other: Int)
Cmn
open String
Cmn
inline operator Dp

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

Cmn

Public properties

Float
Cmn

Extension functions

Unit
Dp.assertIsEqualTo(expected: Dp, subject: String, tolerance: Dp)

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

Cmn
inline Dp
Dp.coerceAtLeast(minimumValue: Dp)

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

Cmn
inline Dp
Dp.coerceAtMost(maximumValue: Dp)

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

Cmn
inline Dp
Dp.coerceIn(minimumValue: Dp, maximumValue: Dp)

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

Cmn
inline Dp
Dp.takeOrElse(block: () -> Dp)

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

Cmn

Extension properties

Boolean

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

Cmn
Boolean

false when this is Dp.Unspecified.

Cmn
Boolean

true when this is Dp.Unspecified.

Cmn

Public companion properties

Hairline

val HairlineDp

A dimension used to represent a hairline drawing element. Hairline elements take up no space, but will draw a single pixel, independent of the device's resolution and density.

Infinity

val InfinityDp

Infinite dp dimension.

Unspecified

val UnspecifiedDp

Constant that means unspecified Dp

Public constructors

Dp

Dp(value: Float)

Public functions

compareTo

open operator fun compareTo(other: Dp): Int

Support comparing Dimensions with comparison operators.

div

inline operator fun div(other: Dp): Float

Divide by another Dp to get a scalar.

div

inline operator fun div(other: Float): Dp

Divide a Dp by a scalar.

div

inline operator fun div(other: Int): Dp

minus

inline operator fun minus(other: Dp): Dp

Subtract a Dp from another one.

plus

inline operator fun plus(other: Dp): Dp

Add two Dps together.

times

inline operator fun times(other: Float): Dp

Multiply a Dp by a scalar.

times

inline operator fun times(other: Int): Dp

toString

open fun toString(): String

unaryMinus

inline operator fun unaryMinus(): Dp

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

Public properties

value

val valueFloat

Extension functions

assertIsEqualTo

fun Dp.assertIsEqualTo(expected: Dp, subject: String, tolerance: Dp = Dp(.5f)): Unit

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
expected: Dp

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

subject: String

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

tolerance: Dp = Dp(.5f)

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

Throws
kotlin.AssertionError

if comparison fails.

coerceAtLeast

inline fun Dp.coerceAtLeast(minimumValue: Dp): Dp

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

Returns
Dp

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

coerceAtMost

inline fun Dp.coerceAtMost(maximumValue: Dp): Dp

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

Returns
Dp

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

coerceIn

inline fun Dp.coerceIn(minimumValue: Dp, maximumValue: Dp): Dp

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

Returns
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.

takeOrElse

inline fun Dp.takeOrElse(block: () -> Dp): Dp

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

Extension properties

isFinite

val Dp.isFiniteBoolean

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

isSpecified

val Dp.isSpecifiedBoolean

false when this is Dp.Unspecified.

isUnspecified

val Dp.isUnspecifiedBoolean

true when this is Dp.Unspecified.