Hey, i'm trying to do something really simple but having some problems. i have a Row(text,icon) and ...
a
Hey, i'm trying to do something really simple but having some problems. i have a Row(text,icon) and the text can be super long so i made it
maxLines = 1
, and
overflow = TextOverflow.Ellipsis
so it can be like. "Lorem ipsum ..." ⬆️ but if i change the
overflow
to
Ellipsis
the text takes the whole row and it becomes -> "Lorem ipsum ..." without the icon i tried
inlineContent
also but that simple thing i couldn't achive anyone has idea?
1
d
You can try to use the
weight(...)
modifier for the text and icon.
✔️ 1
a
if i give weight then won't it be like "Short Text" "icon" i want the icon to be next to text always
c
weight has a fill parameter, that you can set to false so that the Text does not occupy all the available space
Copy code
When fill is true, the element will be forced to occupy the whole width allocated to it. Otherwise, the element is allowed to be smaller - this will result in Row being smaller, as the unused allocated width will not be redistributed to other siblings.
d
Here's a simple working example by using
weight(...)
. Since theres a bit of calculation needed to align the icon perfectly on the right side of the row, I actually hope there's a simpler solution.
Copy code
val screenWidth = LocalContext.current.resources.displayMetrics.widthPixels.toFloat()

val iconSize = with(LocalDensity.current) { 24.dp.toPx() }

val buttonTextRatio = iconSize/screenWidth

Row(modifier = Modifier.fillMaxWidth()) {
    Text(
        modifier = Modifier.weight(1-buttonTextRatio, fill = false),
        text = "hallo",
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )
    Icon(
        modifier = Modifier.weight(buttonTextRatio),
        painter = painterResource(id = R.drawable.ic_baseline_cached_24),
        contentDescription = null
    )
}
a
Thank you guys. fill false made it work. i didn't even need to calculate anything i just gave
text(weight=1,fill=false)
and worked like charm.
👍 1