40

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

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
M_droid
  • 2,447
  • 2
  • 25
  • 35

8 Answers8

23

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.

Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
5

Put it into a LinearLayout and make its gravity "center_horizontal". Then set the chip group's layout_width to "wrap_content". This worked for me.

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center_horizontal">


                <com.google.android.material.chip.ChipGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/langsChipGroup"
                    >

                </com.google.android.material.chip.ChipGroup>

</LinearLayout>
alierdogan7
  • 600
  • 7
  • 14
5

If you have your ChipGroup in a ConstraintLayout, use

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

as such:

<com.google.android.material.chip.ChipGroup
        android:id="@+id/chips"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
...

Be sure not to set the layout_width field to be match_parent, as it wouldn't work that way.

gorilla_glue
  • 315
  • 2
  • 13
1

This library is help you for figure out your query. please check it.and let me know your thoughts for the same.

The ChipCloud library was originally a (very) quickly knocked up Android view for some larger hackathon project by fiskurgit. It creates a wrapping cloud of material ' Chips'. Basic demo of their version is available on the Play Store - I'm maintaining this fork for features I required in it.

Chip Cloud View

Daxesh V
  • 571
  • 6
  • 12
1

I'm writing thinking that it will help someone. I had a similar problem. But I had a single line. And I solved this problem in the following form

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <com.google.android.material.chip.ChipGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="@dimen/padding"
            android:paddingEnd="@dimen/padding"
            app:singleLine="true">

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

//          ...

         </com.google.android.material.chip.ChipGroup>
     </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

I'm glad if I helped someone.

0
  1. Set the width of "com.google.android.material.chip.ChipGroup" to wrap_content ; "match_parent" won't work.

  2. Set the width of each individual chip to "match_parent"

  3. Bonus - to add space between chips add the "app:chipSpacingHorizontal="20dp"" or "app:chipSpacingVertical="20dp"" attribute to the ChipGroup widget.

-6

Put the width of chip group into wrap_content and make the parent view to gravity to center which contains chip group. So that it will be aligned center.

AtHul Antony
  • 77
  • 1
  • 11
-15

Please try following :

layout_gravity="center"

Hope this will help you.