https://kotlinlang.org logo
#compose
Title
# compose
c

chanjungskim

02/28/2023, 4:46 AM
I want to make the android app resizable by screen size. So, I changed the Density like this, is this correct way?
Copy code
val densityDpi = LocalConfiguration.current.densityDpi
    val screenHeightDp = LocalConfiguration.current.screenHeightDp
    val fontScale = LocalDensity.current.fontScale
    val density = (densityDpi / 160f) * (screenHeightDp / 360f)

    CompositionLocalProvider(
        LocalBackgroundTheme provides backgroundTheme,
        LocalDensity provides Density(density, fontScale),
    ) {
        MaterialTheme(
            colorScheme = colorScheme,
            typography = Typography,
            content = content
        )
    }
k

Kirill Grouchnikov

02/28/2023, 4:50 AM
Why do you think this is needed? What does it mean to be resizable by screen size?
c

chanjungskim

02/28/2023, 4:52 AM
my company distributes the app offline and the device is from mobile to desktop. But the design is the same. So, I used to use 'px' but in compose, there's no px. So, I need to make the app resizable like px unit so that It doesn't care how dense the pixels are but fits the screen size.
dp is like cm or inch. So, can't be used in my case because the size in human eye all look the same. And it's not resized by device.
k

Kirill Grouchnikov

02/28/2023, 4:55 AM
You're going to fight every little piece of Compose along the way then
I don't think there's such a thing as a correct way to do something like this, because it's just not the right thing to do in how Compose handles measure units
c

chanjungskim

02/28/2023, 4:57 AM
I thought you gonna say that. but I don't need to.. because the calculation is fine. But not perfect.
k

Kirill Grouchnikov

02/28/2023, 4:58 AM
And this thing is probably not going up respect the user choice in system settings for font and display sizes
c

chanjungskim

02/28/2023, 4:59 AM
It doesn't matter because the users are from company. And we need to fix it.
k

Kirill Grouchnikov

02/28/2023, 5:07 AM
I still don't get what you're getting by working directly with pixels. If your app needs to stretch to fill the whole screen, then it still needs to operate under different pixel dimensions. How is that any different from the regular Android/Compose development where you're doing the same but in dp units? Compose for Desktop also operates in dp units.
c

chanjungskim

02/28/2023, 5:08 AM
Well, I just need to change dp to px.
I think pixel is not equals to px unit. because if you run it in different size of display, it resizes actually.
k

Kirill Grouchnikov

02/28/2023, 5:09 AM
This feels like a classic XY problem to be honest.
This is why I'm trying to focus not on a particular solution that you seem to be focusing on, but instead on what problem you're trying to solve
c

chanjungskim

02/28/2023, 5:16 AM
I don't get it. What's X and Y here?
m

Marcin Wisniowski

02/28/2023, 11:07 AM
Y is all your unit and density conversion code you are trying to write. We don’t know what’s X. What do you actually want your app to do, and how is it different from default behavior?
c

chanjungskim

02/28/2023, 11:22 AM
For example, 1. Same size devices must look the same even if the densities are different. In this case, dp is Okay. 2. Secondly, it should also work with small devices, it must be resized as small as it is. 3. Thirdly, it should also work with bigger devices, it must be resized as big as it is. For example, destop. The size that can be considered are the actual devices' inch or its height. If I need to change all the sizes from Modifier. It will be a lot of work. And I just want the px unit behaviour like in old Android approach.
m

Marcin Wisniowski

02/28/2023, 11:36 AM
Point 1 makes sense to me, but as you said, this is already what Dp does. 2 and 3, do you want to scale everything with the physical size of the screen?
c

chanjungskim

02/28/2023, 11:37 AM
Yes,
m

Marcin Wisniowski

02/28/2023, 11:48 AM
Ok, I see what you mean now, though I’m not sure if that would result in a great experience. If you have a button of reasonable size on a big phone screen, then when you scale that down on a small screen, it will be too small to tap, and all your text will be unreadable. Then when you scale that button up on a desktop monitor, it will take a huge portion of the screen, with huge text.
c

chanjungskim

02/28/2023, 11:52 AM
we don't need the best quality. Our main device is 1920x1080 and other devices are not really serious if it looks fine because it is not used by customers but coworkers.
m

Marcin Wisniowski

02/28/2023, 11:55 AM
In that case you could just throw a
Modifier.graphicsLayer
https://developer.android.com/jetpack/compose/graphics/draw/modifiers#graphics-modifier-scale on the root of your UI to scale everything. It’s more of a zoom than a resize though. Build your UI with Dp so it looks good on 1920x1080 and scale on others. But still, I don’t think you should do that, build a responsive layout and it should work on a range of screen sizes.
k

Kirill Grouchnikov

02/28/2023, 1:28 PM
Copy code
screenHeightDp / 360f
Presumably this magic number comes from targeting a 1920x1080px Android device in landscape mode with 3x density (where 1dp is 3px)? With this approach then, you are hardcoding your entire experience not even to a screen size, but a fixed screen height of 1080 pixels. Again, presumably, you are fine if your UI gets cut out horizontally on screens with lower aspect ratio - where the screen width in landscape mode is less than 1920 pixels? If this is correct and you’re saying that other devices are “not really serious if it looks fine”, why not hardcode your experience to that 640x480dp screen and be done with it?
49 Views