I have some dynamic content and I need to render it as a LazyRow of several Columns. Each Column can have Texts of various lengths.
Ideally, I would like every Column to be the size of the largest Column.
How can I ensure that every Column is the same width?
I think I need IntrinsicSize here but I'm not sure how to apply it in my case:
LazyRow {
items(items = steps) {
Column {
// ... dynamic content, including Texts of varying sizes and lengths
// How can I ensure that EVERY column is the SAME width?
}
}
}
Related
I want the items of the first column to stretch their height depending on the total height of the other column in the row.
Now I have it:
And I want it to be like that:
Is this possible without hardcoding the height through calculations?
Use compose arrangement 'equal weight' according to this documentation link
Modifier.weight()
My current Android project has a number of LazyVerticalGrid lists as follows:-
LazyVerticalGrid(
columns = GridCells.Fixed(LocalListColumnsComposition.current.value),
contentPadding = PaddingValues(0.dp),
verticalArrangement = Arrangement.spacedBy(0.dp),
horizontalArrangement = Arrangement.spacedBy(0.dp),
content = {
items(
items = state,
key = { myUI -> myUI.contentId }) { myUI ->
MyCard(myUI!!, onClick)
}
}
)
my issue is that my list items are different sizes due to their content, which results in a very confusing user experience.
e.g. some items have images, some have text that fills 1 line, others have text that wraps over multiple lines
What I would like is for each of my list items to be a fixed size and still show as much detail as possible
Will I have to measure all items before displaying them and calculate the "standard" dimensions required?
How can I achieve the desired result?
I do not want to use columns = GridCells.Adaptive(minSize = nnn.dp) as my lists have one column in portrait and multiple columns (depending on screen size) in landscape
I have multiple composables (Text) forming a visual column and I would like to make them have the same width (though not necessarily for themselves, but to make column 2 aligned)
The problem is I can't just move the column dimension inside (column of rows -> row of columns) because there are other elements involved (red is set to consume all available space and yellow takes up the whole width):
I've looked into SubcomposeLayout but I can't see how I could fit the whole thing inside and measure the texts.
My current layout looks like this:
Column (parent layout)
Row
Text 1
...
Column with foreach x in 2,3,4
Column
Row
Text x
I only have to work with a limited and static set of texts so worst case, I will add all of them into each field and make all but one invisible, but even a solution where I do this just once, measure it and only apply defaultMinSize to all of them would be nice.
Edit: I don't think this is achievable by simple weights
Just apply the weight modifier:
Column(
) {
Row(Modifier.fillMaxWidth()){
Text("Text1",Modifier.weight(1f).border(1.dp, Black))
Text("Text2",Modifier.weight(1f).border(1.dp, Black))
Text("Text3",Modifier.weight(1f).border(1.dp, Black))
}
Row(Modifier.fillMaxWidth()){
Box(Modifier.fillMaxWidth().height(20.dp).background(Yellow))
}
Row(Modifier.fillMaxWidth()){
Text("Text1",Modifier.weight(1f).border(1.dp, Black))
Text("Text2",Modifier.weight(1f).border(1.dp, Black))
Text("Text3",Modifier.weight(1f).border(1.dp, Black))
}
}
I have a lazy row with items that have wrap content height.
my problem is that some texts of these items is visible or invisible base of that item. so the height of that cart is not fix and it will change.
how can I find the max height of that item (with all texts) and set that height to all items.
I do not want to set a hard code height to my items (like 300.dp)
as you can see in this image: the below button change its position based on the card's height.
I want that button fix in its place and not move up and down.
https://i.stack.imgur.com/R0Tc6.png
how can I fix that problem?
Have you tried experimenting around onGloballyPositioned{..} modifier property?
val localDensity = LocalDensity.current
Text(modifier = Modifier
.onGloballyPositioned { thisText ->
with (localDensity) {
thisText.size.height.toDp()
}
},
text = "Text"
)
Edit: Intrinsic Measurement is not allowed in LazyList components, have a look at this post, looks like similar to yours.
Jetpack Compose: Row with all items same height
Also, Constraintlayout maybe a good thing to experiment on.
One of the rules of Compose is that you should only measure your children once; measuring children twice throws a runtime exception. However, there are times when you need some information about your children before measuring them.
If you have a look at the Android documentation for Intrinsic Measurements, you will have a clear idea of what to do. In your scenario, you need to force Compose to measure the size of your children to adjust the parent composable's dimensions accordingly.
This medium article gives an easier example on how to use IntrinsicSize in Compose.
I have a collection of models that need to be displayed vertically.
Each models has 4 values like strings, that all have a fixed width except the first values.
Can't figure out how to make the first views/columns wrap_content and equal in width compared to each other.
I tried a TableLayout, but that does not keep the widths in different columns for the rows.
I tried to use a MvxLinearLayout with a custom adapter that picks the largest width and sets all other matching views/columns..
Both solutions didn't work, and ended up with to much hacking.
Anyone idea's how to display Android vertical collections with equal/matching column widths?
edit: Collection I'm using has structure like
List<Person>
where Person has { string Name, int Age, string Gender }
The Collection View I'm looking for:
(where all first views in row must be all same size)