Search code examples
androidandroid-layoutandroid-chips

Android how to center align chips in chipgroup?


I tried gravity="center" , foregroundGravity="center" and textAlignment="center" for ChipGroup in the XML file but it won't work. Any ideas?

This is how it looks now


Solution

  • I tried using Chip inside Flexbox and it worked like this.

    <com.google.android.flexbox.FlexboxLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            app:flexWrap="wrap"
            app:justifyContent="center"> ... </com.google.android.flexbox.FlexboxLayout>
    

    enter image description here

    There should be better ways for achieving this but this will work there I guess.

    Update (2021): I removed flexbox dependency due to stability and lack of updates from Google and am achieving the same effect using ConstraintLayout's Flow nowadays, anyone using the technique perhaps can consider that also, have a look at https://stackoverflow.com/a/61545990 to fill it programmatically.