Hi all, I am trying to dynamically adjust the space between the components in my content depending o...
u
Hi all, I am trying to dynamically adjust the space between the components in my content depending on the visible screen height (keyboard up or not). My content consists of multiple components that are optional, so it could be somewhere between 2 to 10 components. I Don't want to change the height of my components, but only the space between them. So, I would like to have something like min and max height for the spacers in between, so that max height is used when the screen has it. Then it should start to shrink the height slowly as necessary until it reaches min value. Then it should start to scroll after reaching the min value. I am trying to solve 2 problems here; 1. When the keyboard is up. 2. Phones with smaller screens. Any suggestions please..
a
This sounds specific enough that I would write a custom layout using the
Layout
composable.
Modifier.verticalScroll
(or horizontal if needed) can provide the scrolling behavior once the layout itself is written.
If you only wanted the arrangement to differ and not the resulting measured size, a custom
Arrangement
for a row or column would do the trick
But since you describe max sizes for the space in between items, that affects some of the sizing behavior of the overall layout and might make a fully custom layout a better choice. Maybe try both and see which results you prefer
u
Hey @Adam Powell, thanks for your response and the suggestions. I would like to avoid building custom layouts myself if there is a solution, but would try out both. How would i get the height of the visible screen when keyboard is up?
a
Generally you would use the max height constraint of the layout's measurement. If you have something like a chat text entry field below this layout, you would want to act on the usable space minus those other UI elements that also consume height, which the max constraint will give you
u
Thanks Adam.
I see that max height is available in
BoxWithConstraints
scope. When i try to get the maxHeight, it is the same irrespective of whether the keyboard is up or down. Am i doing something wrong here?
a
Make sure the activity's soft input mode is set to adjustResize. You can do this from the activity's manifest entry
u
That worked thanks.
It looks like this maxHeight, used right, could solve my issue. What are other Composables that have ‘maxHeight’?
a
Layout
is what you are likely to want here.
BoxWithConstraints
is a fairly heavyweight tool if you aren't changing the structure of the content you're composing such as switching between phone and tablet layouts based on available space
I'd you're just adjusting sizing and positioning of the same elements regardless, Layout is simpler and more efficient.
u
Oh, ok. Good to know. Thanks.
Will try my solution with a layout then.