1

Suppose I have a screen in figma with 1080x1920 resolution, then I add 20px padding, when I recreate this layout with compose I need to parse 20 to dp (using some utility function like: val Int.dp: Int get() = (this * Resources.getSystem().displayMetrics.density + 0.5f).toInt()) or can I just use 20.dp?

Currently I only use .dp extension and I never had a problem.

4
  • 2
    What do you mean 20 px padding? In Android, px is actual pixels. That means it will change size based on the device. You should never use px to space things, px should only really be used with canvas draw commands. Instead, decide how big you want the spacing in physical size (inches) and convert that to dp (160 dp= 1 inch). Use that to convert using .dp Commented Dec 12, 2023 at 18:54
  • 1
    THis is opposed to languages like html/css where you don't have access to actual pixels, and px is the equivalent of dp in android (except they use 96 px in an inch, where android uses 160 dp). Commented Dec 12, 2023 at 18:55
  • In this case, 20px refers to space 20 in the figma layout frame with 1080x1920 resolution, my question is how can I adjust this for the mobile screen. Commented Dec 13, 2023 at 18:39
  • 1
    1080x1920 resolution means nothing. It's just a size. That could be 2cm wide, or 2 km wide. It depends on the dpi. So your question is unansweable as is. Decide how big you want the space to be in physical units, and use dp. Your problem is you're thinking about everything in the wrong way. What you want to be thinking about is how to lay things out in matter that scales to screensize, not "how do I translate these exact numbers" Commented Dec 13, 2023 at 19:22

1 Answer 1

2

You don't have to parse anything manually. If you need to set a padding, width or height to a composable via Modifier, then using the Dp implementation is the way to go. But if you need to paint something on a Canvas or do some low-level layout measurements e.g. using constraints to define an Offset, then converting a dp to px value is the way to go. Such conversion can be done by using the toPx(), toDp(), and many more Density methods. Keep in mind that these methods are accessible only within a Density context.

Here is a basic application of a padding using dp:

Text( modifier = Modifier.padding(horizontal = 16.dp), text = "This text has a horizontal padding of 16." ) 

An example of drawing a square on a Canvas using dp to px conversion:

Canvas( modifier = Modifier .width(200.dp) .height(100.dp), onDraw = { // Convert dp to px. val squareSize = 50.dp.toPx() drawRect( color = Color.Blue, size = Size( width = squareSize, height = squareSize ) ) } ) 

And if you need to convert values when Density context is not provided:

val spacing = with(LocalDensity.current) { 16.dp.toPx() } SubcomposeLayout { constraints -> /// ... layout( width = constraints.maxWidth, height = constraints.maxHeight ) { /// ... val itemOffsetX = previousItemOffsetX + spacing } /// ... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.