Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose row items next line based on dynamic width

I have routes in a (stop's)row, and I would like to continue showing them on the following line if they achieve the edge of the screen. Could you please help me with that?

@Composable
fun StopView(
    stop: Stop
) {
    Column {
        Text(text = stop.name)
        Row{
            stop.routes.forEach { route ->
                Row (modifier = Modifier.padding(10.dp).align(Alignment.CenterVertically)) {

                    Image(painterResource(id = **imageName**),route.type.toString(), modifier = Modifier
                        .height(25.dp)
                        .width(25.dp))
                    Spacer(modifier = Modifier.width(5.dp))
                    Text(text = route.name)
                }


            }
        }
    }
}
like image 856
sandorb Avatar asked Feb 04 '26 13:02

sandorb


2 Answers

I successfully solved it with Jetpack Compose Flow Layouts from Accompanist library.

like image 156
sandorb Avatar answered Feb 06 '26 03:02

sandorb


You can use the FlowRow composable.

https://developer.android.com/jetpack/compose/layouts/flow

@Composable
private fun FlowRowSimpleUsageExample() {
    FlowRow(modifier = Modifier.padding(8.dp)) {
        ChipItem("Price: High to Low")
        ChipItem("Avg rating: 4+")
        ChipItem("Free breakfast")
        ChipItem("Free cancellation")
        ChipItem("£50 pn")
    }
}

FlowRow and FlowColumn are composables that are similar to Row and Column, but differ in that items flow into the next line when the container runs out of space. This creates multiple rows or columns.

like image 36
Magnus Lundberg Avatar answered Feb 06 '26 05:02

Magnus Lundberg