Reputation: 2215
In Android Compose I want to create a Row
which occupies all available width and contains two texts:
I tried my best to do this, but I can only achieve either of these things:
a. Make the second text go right after the first one but overflow first (by simply placing them one after another in the code).
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = title)
Text(
text = "Fixed text",
maxLines = 1
)
}
b. Make the first text overflow first, but occupy the whole available space when it doesn't overflow (by adding Modifier.weight(1f)
to the first text).
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = title,
modifier = Modifier.weight(1f)
)
Text(
text = "Fixed text",
maxLines = 1
)
}
How can I achieve the described behavior? I know I can probably use ConstraintLayout
but I would like to avoid it if possible.
Upvotes: 6
Views: 1132
Reputation: 3179
You can use the fill
parameter of the weight
, by default it set to true, therefore automatically occupies all available space. But if you switch to false you will get what you want.
Example:
// PreviewShortTextWithNoFill
Row(
modifier = Modifier
.fillMaxWidth()
.padding(all = 16.dp),
horizontalArrangement = Arrangement.spacedBy(space = 8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
modifier = Modifier.weight(weight = 1F, fill = false),
text = "random short text"
)
Text(
text = "fixed text",
maxLines = 1
)
}
Upvotes: 12