Material-components-android: [ChipGroup] Force one selected chip with `app:singleSelection="true"`

Created on 5 Oct 2019  路  3Comments  路  Source: material-components/material-components-android

When you define a ChipGroup with app:singleSelection="true" you allow a single chip to be checked at a time.
It would be nice to have an attribute to avoid the possibility to have no one item selected. Something like app:forceOneSelectedChip="true". In this case the app:checkedChip should be mandatory.

Currently the only workaround is quite ugly.

chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
      @Override public void onCheckedChanged(ChipGroup group, int checkedId) {
           //iterate through the children of the ChipGroup to set chip.setClickable(false); in the selected chip
      }
    });

Also the same attribute could be used in a multi selection scenario.
In this case changes the meaning in something like app:atLeastOneSelectedItem="true" but it works in the same way, just to prevent having no one item selected.

feature request

Most helpful comment

@thsaravana starting from 1.2.0-alpha02

All 3 comments

@gabrielemariotti Hi thanks for the feature request, we will be adding it to both MaterialButtonToggleGroup and ChipGroup. We went with the attribute name selectionRequired, hopefully that's clear enough.

We also opted to not crash if app:checkedChip is not provided. Emulating RadioGroup behavior if that makes sense.

@ymarian In which release is this fixed ?

@thsaravana starting from 1.2.0-alpha02

Was this page helpful?
0 / 5 - 0 ratings