Could you please point me to resources about respo...
# compose
n
Could you please point me to resources about responsive layouts in Jetpack Compose? I have a UI which looks right on XL screens but really huge on smaller screens. I put my layout in a Column with max width and height so I thought the layout would be responsive in that sense, but it does not appear to be the case...
r
Can you define “responsive” here? Compose, like the existing Android UI toolkit, relies on layouts that by default will use whatever available space. So they are responsive in that sense.
n
Yes, it's strange though, because part of my UI is cut off when using the Nexus 4 emulator. So I don't think I am doing things right. That's why I was looking for some docs...
In Flutter I would use Media Query
but here I'm not sure
@romainguy
r
Well it depends on the behavior you’d like to have here. Would you like a different layout on the smaller screen? In this particular case just wrapping everything in a ScrollableColumn could be enough
n
Thanks, I'll settle for this option. If I wanted to shrink the layout so it would fit without scrolling, what would I use? @romainguy
I guess constraints
z
Which parts of your layout do you want to shrink? Can you post your layout code?
c
Would you like to make the squares smaller so that they fit without crowding? Constraints with percent could be a nice fit 🙂
👍 1
n
Thanks for your reply. @Zach Klippenstein (he/him) [MOD]. Basically, I would like my layout to fit the smaller screen. I'm a beginner with JC, so do let me know if this is not the right place to ask for help! :-)
@caelum19 Sounds good, I guess I could use them for the whole layout? 🙂
c
Yeah here is a quick and messy example using ConstraintLayout:
probably not the cleanest way to do it tbh
n
@caelum19 Many thanks, I'll give this a try! 👍🙂
z
You also don’t need constraint layout to do this, just don’t hard-code your sizes. So instead of
Copy code
Card(
    shape = RoundedCornerShape(20.dp),
    backgroundColor = Color(0xffFFAB91),
    modifier = Modifier
        .clickable(onClick = { })
        .height(150.dp)
        .width(150.dp)
        .padding(10.dp)
) {
You could, for example, do something like:
Copy code
Card(
    shape = RoundedCornerShape(20.dp),
    backgroundColor = Color(0xffFFAB91),
    modifier = Modifier
        .clickable(onClick = { })
        .padding(10.dp)
        .aspectRatio(1f)
        .fillMaxSize()
) {
👍 3
c
There's also the
weight
Modifier that helps size things relative, similar to percentage (which we don't have yet). Also the
ConfigurationAmbient
is available to query the device dimensions that you can use inline to decide how to size things.
👍 1
z
Good point, I think
fillMaxSize()
might not actually work here,
weight
is better. Wouldn’t it be better to use
WithConstraints
instead of
ConfigurationAmbient
for this case, since it probably matters more what the constraints of this actual grid are, not the real screen size?
👍 1