2

I have a chipgroup and some chips inside. I loop through this chipgroup, get every chip and add a checkedChangeListener for it.

for (int i = 0; i < chipGroup.getChildCount(); i++) {
            final Chip chip = (Chip) chipGroup.getChildAt(i);
            chip.setOnCheckedChangeListener((buttonView, isChecked) -> {
                 //My code
            });
        }

So when I open my app and click on chip, e.g. chip1, it selects it. Then when I click on that chip1 second time, it also fires the checkedChangeListener and runs my code inside it. The isChecked status is not helping, because it just changes it value like true->false->true->false on every click. But I just don't want to run my code inside onCheckedChangeListener, if the chip is already checked. One solution I think is to save my selected chip inside fragment or ViewModel. What's your solution?

Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61
  • Why wouldn't you be able to simply use the isChecked value? If that is false, then previously it was true, right? Or do you want a user to check it only once? You might find [this answer](https://stackoverflow.com/questions/31640784/how-to-flip-the-checked-status-of-a-switch-only-after-confirm-dialog) from Evgeniy helpful. It's similar to your question, but for a Switch in Android. – Jorn Rigter Mar 09 '21 at 16:28
  • That's because imagine user clicks on chip1 first time, the result is now true, then he clicks the second time, the result is false, and now user clicks the third time, the result now true. So user perform a click on the same chip 3 times. What I want is, when user clicks on the same chip second time, just don't call onCheckedChangeListener, or don't evaluate the code in it. Something like this. – Hayk Mkrtchyan Mar 09 '21 at 17:59

2 Answers2

1

I believe you can just set a setOnCheckedChangeListener on the chipGroup instead of each individual chip. That listener will pass in an ID and isChecked as parameters. I believe using this callback on the group will be help resolve your issue. Hopefully that helps with the issue you're having.

More info on https://material.io/components/chips/android#using-chips

xr100chris
  • 184
  • 4
  • Thanks, I'll have a look on this. – Hayk Mkrtchyan Mar 09 '21 at 18:00
  • But how can I determine the isChecked status? – Hayk Mkrtchyan Mar 09 '21 at 18:02
  • You see I need something alternative. When user performs the click on chip, that chip already changes its value to true and I am not able to determine does the chip was checked before that click or not. I think I need to save the currently selected chip somewhere to check it after performing the click. – Hayk Mkrtchyan Mar 09 '21 at 18:05
  • The listener method passes the ChipGroup. So you should be able to reference `group.getCheckedChipIds`. Then traverse that list to see if it contains the `checkedId`. – xr100chris Mar 09 '21 at 18:17
  • Well you can do that by negating the value you retrieve from it being selected or not. If you grab the `group.getCheckedChipIds` and the chekedId exists in the list, negate that value and that should give you what it was before it changed. If that makes sense. Now if you're trying to keep track of all changes, then you might use a map or something to keep track. – xr100chris Mar 09 '21 at 19:44
  • I've figured out. So imagine I have 3 chips. When I click on Chip 1, the isChecked becomes true. It's ok. Now when I click on chip 2, the chipCheckChangedListener triggered 2 times. One time it's trigger for chip1, where it changes the isChecked value for chip1 to false, and second trigger for chip 2, which makes the isChecked to true. And when I click on chip2 second time, it also triggered twice. One time the chip2 isChecked becomes false, the second time isChecked becomes true. The solution now is to save the last selected chip id in my fragment and add appropriate checks. – Hayk Mkrtchyan Mar 13 '21 at 17:00
0

you can do this in ChipGroup XML Layout:

 <com.google.android.material.chip.ChipGroup
 style="@style/Chip_group_style"
 app:selectionRequired="true"
 >
Amin Pro
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 12:00