Evgeniy  Rechkov
Evgeniy Rechkov

Reputation: 515

Android Compose set height of view in pixels NOT in dp

I want to set the height of view in pixels not in dp.

 `Box(modifier = Modifier.height(100.dp))`

In this example height of the box is set to 100 dp and modifier function accepts only dp. How to set height of Box in pixel?

Upvotes: 14

Views: 6512

Answers (2)

Phil Dukhov
Phil Dukhov

Reputation: 88277

@Kilian it right, this can look like this:

Modifier.height(with(LocalDensity.current) { 100.toDp() })

Upvotes: 19

Kilian
Kilian

Reputation: 295

You can use the toDp() method provided in the Density package.

See https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Density#(kotlin.Int).toDp() for more information.

Upvotes: 3

Related Questions