My layout consists of 2 texts with an arrow image between them. All elements are placed in a composable Row and aligned to the left, right behind eachother. When the texts are short it looks like this:
|Origin → destination |
When the texts are too long, I'd like them to ellipsise with max 1 line. If the 'destination' is too long, it works as expected:
|Origin → looooooooongDest...|
But what if the 'origin' text is too long?
|LooooooooooooooongOrigin → .|
<<< wrong!
In xml ConstraintLayout I'd use a layout_constraintWidth_percent=0.5 for the first text, which results in the following:
|Loooooooo... → destination |
Simply put, this is my request:
- Texts and icons are all aligned left, after eachother
- Texts are always 1 line max and ellipsized if too long
- The left text is max 50% of the total row width
- The right text can fill the rest of the width
How can I achieve this in compose?
Note: I've tried using combinations of .weight(0.5F)
and .weight(0.5F, fill = false)
on either the first or both texts. These are some of the results I got with these solutions, which is not what want either, but I think I'm getting closer to the desired result with these modifiers:
|Origin → destination |
<<< wrong!
|Or.. → looooooongDestination|
<<< wrong!
My latest code is getting close to the desired solution:
Row {
Text(
modifier = Modifier.weight(0.5F, fill = false),
text = "Origin",
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Icon(
modifier = Modifier.padding(horizontal = 4.dp),
...
)
Text(
modifier = Modifier.weight(0.5F),
text = "Destination",
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
Although this seems to work as expected for the 1st text, it always truncates the 2nd text to 50% width:
|Origin → loooongDest... |
<<< wrong!