Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any ideas on the list with fit/wrap width on item in Jetpack compose

I need to implement next UI element:

enter image description here

  • Unknown size list of strings
  • Any item should be wrap content.
  • If an item is not fitted to row, he will be in the next row.
  • All list/grid is centered
like image 913
Ashraf Patel Avatar asked Apr 04 '20 00:04

Ashraf Patel


People also ask

What is mutableStateOf in jetpack compose?

mutableStateOf creates an observable MutableState<T> , which is an observable type integrated with the compose runtime. interface MutableState<T> : State<T> { override var value: T } Any changes to value will schedule recomposition of any composable functions that read value .

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.

What is scaffold in jetpack compose?

A Scaffold is a layout which implements the basic material design layout structure. You can add things like a TopBar, BottomBar, FAB or a Drawer.

How do I make my screen scrollable in jetpack compose?

We can make the Column scrollable by using the verticalScroll() modifier.


1 Answers

You can use FlowRow from accompanist-flowlayout to implement this. It renders its children horizontally (like Row) but also wraps them by moving to the new line if they don't fit in the existing line. It also allows to configure horizontal and vertical spacing between items.

To nicely handle very long strings (that will not fit into single line themselves) you can set overflow = TextOverflow.Ellipsis and maxLines = 1 on Text.

@Composable
fun HashTagList(hashTags: List<String>) {
    FlowRow(
        modifier = Modifier.padding(8.dp),
        mainAxisAlignment = MainAxisAlignment.Center,
        mainAxisSize = SizeMode.Expand,
        crossAxisSpacing = 12.dp,
        mainAxisSpacing = 8.dp
    ) {
        hashTags.forEach { hashTag ->
            Text(
                text = hashTag,
                modifier = Modifier
                    .background(
                        color = colorForHashTag(hashTag),
                        shape = RoundedCornerShape(4.dp)
                    )
                    .padding(8.dp),
                overflow = TextOverflow.Ellipsis,
                maxLines = 1
            )
        }
    }
}

enter image description here

like image 89
Maciej Ciemięga Avatar answered Sep 28 '22 22:09

Maciej Ciemięga