https://kotlinlang.org logo
l

loloof64

11/25/2020, 9:32 AM
Hi ! Is there a simple way to convert size (is given as dp) to sp unit ?
Copy code
val cellsSize = with(DensityAmbient.current) {
   (size.toIntPx() * 0.1111) // Can I convert this result (which is px in Int) into an Sp unit, for a text ?
}
I've been searching at https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/package-summary. But did not find which one could be useful.
a

André Kindwall

11/25/2020, 9:35 AM
Copy code
val cellsSize = with(DensityAmbient.current) {
    (size.toPx() * 0.1111f).toSp()
}
👍 1
l

loloof64

11/25/2020, 9:35 AM
Thank you very much 😃 I've done a CTRL+F though.
In fact I should even write
Copy code
val cellsSize = with(DensityAmbient.current) {
        Dp((size.toIntPx() * 0.1111).toFloat()).toSp()
    }
Because toSp() is available from a Dp unit.
a

André Kindwall

11/25/2020, 9:40 AM
Copy code
val cellsSize = with(DensityAmbient.current) {
    (size * 0.1111f).toSp()
}
This is even better
👍 1
l

loloof64

11/25/2020, 10:23 AM
Yes, thank you