Search code examples
androidmaterial-componentsmaterial-components-androidandroid-chips

ChipGroup single selection


How can I force a ChipGroup to act like a RadioGroup as in having at least one selected item always? Setting setSingleSelection(true) also adds the possibility to have nothing selected if you click twice on a Chip.


Solution

  • To prevent all chips from being deselected you can use the method setSelectionRequired:

    chipGroup.setSelectionRequired(true)
    

    You can also define it in the layout using the app:selectionRequired attribute:

    <com.google.android.material.chip.ChipGroup
        app:singleSelection="true"
        app:selectionRequired="true"
        app:checkedChip="@id/..."
        ..>
    

    Note: This requires a minimum of version 1.2.0