https://kotlinlang.org logo
#getting-started
Title
# getting-started
p

Pragnesh Meniya

11/22/2023, 5:42 AM
How to handle the DP with Compose, In XML-based views, I provide multi-screen support by using the SDP library (https://github.com/intuit/sdp), but I don't know how to handle DP with Compose. Can someone help me with how to handle this?
a

ascii

11/22/2023, 6:14 AM
That lib — and ssp — defines its dimensions in values-sw* folders. There's nothing stopping you from defining your own set of units. You can use window utilities provided by
androidx.compose.material3:material3-window-size-class
or, again, define your own. In my opinion though, if you're simply scaling up UI to fit larger screens, you're wasting the fact that you have more screen real estate to work with. Displaying the same amount of info regardless of available space feels wrong.
d

Divyansh Kushwaha

11/22/2023, 10:05 AM
You can use something like this. If I remember correctly, intuit used 360 or similar as BASE_SCREEN_WIDTH
Copy code
inline val Int.sdp: Dp
    @Composable get() {
         val configuration = LocalConfiguration.current
         val screenWidth = configuration.screenWidthDp.toFloat()
         return screenWidth.div(BASE_SCREEN_WIDTH).times(this).dp
    }
However, I do agree with @ascii. Instead of just scaling the content, we should use the available space judiciously.