Search code examples
androidxmlmaterial-designmaterial-components-androidandroid-chips

Android: set default choice chip


Is there any way to set a choice chip as a default option? Furthermore, not to be able to uncheck any chip if there is only one chip selected?


Solution

  • You can use these attributes in the ChipGroup component:

    • app:singleSelection="true". In this way the ChipGroup can be configured to only allow a single chip to be checked at a time using
    • app:checkedChip to define a default choice

    Something like:

            <com.google.android.material.chip.ChipGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:singleSelection="true"
                app:checkedChip="@id/ch2">
    
                <com.google.android.material.chip.Chip
                    style="@style/Widget.MaterialComponents.Chip.Choice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/ch1"
                    android:text="M"/>
    
                <com.google.android.material.chip.Chip
                    style="@style/Widget.MaterialComponents.Chip.Choice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/ch2"
                    android:text="F"/>
    
            </com.google.android.material.chip.ChipGroup>