How to convert TextUnit to Dp in Jetpack Compose?

Trinity

I know the difference between them. I want to calculate the text height base on lineHeight. The value of lineHeight is in TextUnit so I want to convert it into Dp.

Ryan M

You need to grab the current Density from the AmbientDensity—so this will only work in composition, within a @Composable function—and use that to convert to Dp:

val lineHeightSp: TextUnit = 12.sp
val lineHeightDp: Dp = with(AmbientDensity.current) {
     lineHeightSp.toDp()
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to handle navigation in Jetpack Compose?

From Java

How can I get onTouchEvent in Jetpack Compose?

From Dev

How to add Image in Android using Jetpack Compose

From Dev

How to know if Text is visible on Jetpack Compose?

From Dev

How to convert PX into DP in Layout

From Dev

How to convert PX into DP in Layout

From Dev

How can I achieve this Button Animation in Jetpack Compose?

From Dev

How to enter text in Jetpack compose TextField through UI tests?

From Dev

How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?

From Dev

How to change the outline color of OutlinedTextField from jetpack compose?

From Dev

How can I achieve this Button Animation in Jetpack Compose?

From Java

Center composable in Jetpack compose

From Dev

ProvideEmphasis not found in jetpack compose

From Dev

ProvideEmphasis not found in jetpack compose

From Dev

Jetpack Compose – LazyColumn not recomposing

From Dev

How can i convert my recursive DP solution to iterative DP?

From Java

How to convert px to dp in margins programmatically?

From Dev

How to convert dp value between devices?

From Java

Android Jetpack compose IconButton padding

From Java

Jetpack compose breaks Room compiler

From Dev

Jetpack Compose list diffs animation

From Dev

Aligning a resized TextField in Jetpack Compose

From Dev

Jetpack Compose draw on image with Painter

From Dev

Create chip with outline Jetpack Compose

From Dev

How to convert dp to pixels and and use it to draw to canvas in android

From Dev

How do i convert the following recursive dp to iterative

From Java

How to convert DP, PX, SP among each other, especially DP and SP?

From Java

Material icon size adjustment in Jetpack Compose?

From Java

Jetpack Compose State: Modify class property

Related Related

  1. 1

    How to handle navigation in Jetpack Compose?

  2. 2

    How can I get onTouchEvent in Jetpack Compose?

  3. 3

    How to add Image in Android using Jetpack Compose

  4. 4

    How to know if Text is visible on Jetpack Compose?

  5. 5

    How to convert PX into DP in Layout

  6. 6

    How to convert PX into DP in Layout

  7. 7

    How can I achieve this Button Animation in Jetpack Compose?

  8. 8

    How to enter text in Jetpack compose TextField through UI tests?

  9. 9

    How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?

  10. 10

    How to change the outline color of OutlinedTextField from jetpack compose?

  11. 11

    How can I achieve this Button Animation in Jetpack Compose?

  12. 12

    Center composable in Jetpack compose

  13. 13

    ProvideEmphasis not found in jetpack compose

  14. 14

    ProvideEmphasis not found in jetpack compose

  15. 15

    Jetpack Compose – LazyColumn not recomposing

  16. 16

    How can i convert my recursive DP solution to iterative DP?

  17. 17

    How to convert px to dp in margins programmatically?

  18. 18

    How to convert dp value between devices?

  19. 19

    Android Jetpack compose IconButton padding

  20. 20

    Jetpack compose breaks Room compiler

  21. 21

    Jetpack Compose list diffs animation

  22. 22

    Aligning a resized TextField in Jetpack Compose

  23. 23

    Jetpack Compose draw on image with Painter

  24. 24

    Create chip with outline Jetpack Compose

  25. 25

    How to convert dp to pixels and and use it to draw to canvas in android

  26. 26

    How do i convert the following recursive dp to iterative

  27. 27

    How to convert DP, PX, SP among each other, especially DP and SP?

  28. 28

    Material icon size adjustment in Jetpack Compose?

  29. 29

    Jetpack Compose State: Modify class property

HotTag

Archive