Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a ChipGroup to act like a radioGroup?

How do I make a ChipGroup to act like a radioButton where one item can be selected at a time while changing the background color.

Image Screenshot

I saw this link for something like this but it did not help me since because am using a layoutInflater to show my chip item.

firebaseFirestore.collection("Categories").addSnapshotListener((queryDocumentSnapshots, e) -> {
            for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){
                if (doc.getType() == DocumentChange.Type.ADDED){
                    Categories categories = doc.getDocument().toObject(Categories.class);
                    post_list.add(categories);
                    Chip chip = (Chip) getLayoutInflater().inflate(R.layout.chip_item_layout, chipGroup, false);
                    chip.setText(categories.getTitle());
                    chipGroup.addView(chip);
                    chipGroup.setOnCheckedChangeListener((chipGroup, id) -> {
                        Chip chip2 = ((Chip) chipGroup.getChildAt(chipGroup.getCheckedChipId()));
                        if (chip2 != null) {
                            for (int i = 0; i < chipGroup.getChildCount(); ++i) {
                                chipGroup.getChildAt(i).setClickable(true);
                                chip2.setChipBackgroundColorResource(R.color.customOrange);
                            }
                            chip2.setClickable(false);
                        }
                    });

                }
            }
        });
like image 964
bensofter Avatar asked Oct 17 '25 11:10

bensofter


1 Answers

In your ChipGroup use the app:singleSelection="true" attribute. In this way the ChipGroup can be configured to only allow a single chip to be checked at a time.

<com.google.android.material.chip.ChipGroup
    app:singleSelection="true"
    ..>

Then you can set a selector color using the app:chipBackgroundColor attribute in your layout chip_item_layout.xml.

Something like:

<com.google.android.material.chip.Chip
    style="@style/Widget.MaterialComponents.Chip.Choice"
    app:chipBackgroundColor="@color/chip_background_color"
    ..>

Pay attention to style="@style/Widget.MaterialComponents.Chip.Choice" because it defines the chip as android:checkable="true".

The chip_background_color is a selector where you can define your favorite colors in the different states. It is the default selector, you can change it:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- 24% opacity -->
  <item android:alpha="0.24" android:color="?attr/colorPrimary" android:state_enabled="true" android:state_selected="true"/>
  <item android:alpha="0.24" android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
  <!-- 12% of 87% opacity -->
  <item android:alpha="0.10" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>

</selector>

The selected chip is define by the color in your case is the first line (android:state_selected="true").

If you would like to do it programmatically just use (not in the OnCheckedChangeListener) the setChipBackgroundColorResource method.

chip.setChipBackgroundColorResource(R.color.chip_background_color);

enter image description here

Also if you want to require at least one selection you can use the app:selectionRequired attribute. This attribute requires the 1.2.0 (starting from 1.2.0-alpha02)

like image 178
Gabriele Mariotti Avatar answered Oct 19 '25 02:10

Gabriele Mariotti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!