Rahul
Rahul

Reputation: 641

Select and deselect all chips inside ChipGroup programmatically in Android

I have a chip group and inside that I am adding Choice Chips programmatically and I have a button called Select All for selecting all if some of them selected and Same Button for Deselecting All chips in a single click.

Now Please guide me some proper way or It would be great if it can be done using chip group instead of ArrayList of chip Thanks in advance :)

Upvotes: 1

Views: 5336

Answers (4)

avez raj
avez raj

Reputation: 2145

Clears the selection. When the selection is cleared, no chip in this group is selected chipgroup.clearCheck()

Upvotes: 0

live-love
live-love

Reputation: 52366

ChipGroup chipGroup = view.findViewById(R.id.chipGroup );
for (int i = 0; i < chipGroup.getChildCount(); i++) {
    Chip chip = (Chip) chipGroup.getChildAt(i);
    chip.setChecked(false);
}

Upvotes: 1

Rahul
Rahul

Reputation: 641

I have created a general Extension function for the above solution in kotlin I think that this is the proper solution

fun ChipGroup.applyCheckedOnAll(isChecked: Boolean){
    if (isChecked){
        for (index in 0 until this.childCount) {
            val chip:Chip = this.getChildAt(index) as Chip
            chip.isChecked = true
        }
    }else {
        this.clearCheck()
    }
}

Upvotes: 3

Naveen Rao
Naveen Rao

Reputation: 742

For deselecting you can use clearCheck and for selection you've to go through for loop

Upvotes: 7

Related Questions