Search code examples
androidmaterial-designandroid-chips

Is it possible to set Material ChipGroup orientation to VERTICAL?


I'm adding a material ChipGroup dynamically to a parent linear layout which is set to VERTICAL orientation but the chip items seem to be added horizontally. Is there any way to make it lay out the chip items vertically?

setLayoutDirection() method of ChipGroup class method doesn't seem to take any parameter that supports VERTICAL orientation.


Solution

  • No, it's not possible using ChipGroup. ChipGroup extends a simple FlowLayout, which was designed to layout items side to side (and then in another line). You can find ChipGroup here: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/chip/ChipGroup.java

    FlowLayout is not that complex. If I were you, I would just copy the sources and modify FlowLayout.onLayout() to layout children vertically. If you don't need extra features (like selection tracking), you can just use FlowLayout without modifying ChipGroup. If you need a really simple stack of chips, you can simply use a vertical LinearLayout.