Skip to content

Commit a05b3a5

Browse files
matpagymarian
authored andcommitted
[ChipGroup] Added a method to get the selected ids
Resolves #667 GIT_ORIGIN_REV_ID=185fec8087c2a9c7186a2565330c797dd994dfb8 PiperOrigin-RevId: 274013304
1 parent ecaa6f6 commit a05b3a5

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

lib/java/com/google/android/material/chip/ChipGroup.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import android.widget.CompoundButton;
3636
import com.google.android.material.internal.FlowLayout;
3737
import com.google.android.material.internal.ThemeEnforcement;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
3841

3942
/**
4043
* A ChipGroup is used to hold multiple {@link Chip}s. By default, the chips are reflowed across
@@ -257,19 +260,50 @@ public void check(@IdRes int id) {
257260
* @return the unique id of the selected chip in this group in single selection mode
258261
* @see #check(int)
259262
* @see #clearCheck()
263+
* @see #getCheckedChipIds()
260264
* @attr ref R.styleable#ChipGroup_checkedChip
261265
*/
262266
@IdRes
263267
public int getCheckedChipId() {
264268
return singleSelection ? checkedId : View.NO_ID;
265269
}
266270

271+
/**
272+
* Returns the identifiers of the selected {@link Chip}s in this group. Upon empty
273+
* selection, the returned value is an empty list.
274+
*
275+
* @return The unique IDs of the selected {@link Chip}s in this group. When in {@link
276+
* #isSingleSelection() single selection mode}, returns a list with a single ID. When no
277+
* {@link Chip}s are selected, returns an empty list.
278+
* @see #check(int)
279+
* @see #clearCheck()
280+
* @see #getCheckedChipId()
281+
*/
282+
@NonNull
283+
public List<Integer> getCheckedChipIds() {
284+
ArrayList<Integer> checkedIds = new ArrayList<>();
285+
for (int i = 0; i < getChildCount(); i++) {
286+
View child = getChildAt(i);
287+
if (child instanceof Chip) {
288+
if (((Chip) child).isChecked()) {
289+
checkedIds.add(child.getId());
290+
if (singleSelection) {
291+
return checkedIds;
292+
}
293+
}
294+
}
295+
}
296+
297+
return checkedIds;
298+
}
299+
267300
/**
268301
* Clears the selection. When the selection is cleared, no chip in this group is selected and
269302
* {@link #getCheckedChipId()} returns {@link View#NO_ID}.
270303
*
271304
* @see #check(int)
272305
* @see #getCheckedChipId()
306+
* @see #getCheckedChipIds()
273307
*/
274308
public void clearCheck() {
275309
protectFromCheckedChange = true;

lib/javatests/com/google/android/material/chip/ChipGroupTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,25 @@ public void testSelection() {
6565
chipgroup.clearCheck();
6666
assertThat(chipgroup.getCheckedChipId()).isEqualTo(View.NO_ID);
6767
}
68+
69+
@Test
70+
public void testMultipleCheckedChip() {
71+
int chip1Id = chipgroup.getChildAt(0).getId();
72+
chipgroup.check(chip1Id);
73+
int chip2Id = chipgroup.getChildAt(1).getId();
74+
chipgroup.check(chip2Id);
75+
assertThat(chipgroup.getCheckedChipIds()).hasSize(2);
76+
}
77+
78+
@Test
79+
public void testSingleCheckedChip() {
80+
chipgroup.setSingleSelection(true);
81+
int chipId = chipgroup.getChildAt(2).getId();
82+
chipgroup.check(chipId);
83+
assertThat(chipgroup.getCheckedChipIds()).hasSize(1);
84+
//check that for a single checked chip methods should be equivalent
85+
Integer checkedId1 = chipgroup.getCheckedChipIds().get(0);
86+
int checkedId2 = chipgroup.getCheckedChipId();
87+
assertThat(checkedId1).isEqualTo(checkedId2);
88+
}
6889
}

lib/javatests/com/google/android/material/chip/res/layout/test_reflow_chipgroup.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,21 @@
2323
android:layout_height="wrap_content"
2424
android:layout_margin="16dp">
2525
<com.google.android.material.chip.Chip
26-
xmlns:android="http://schemas.android.com/apk/res/android"
27-
android:id="@+id/filter_chip"
26+
android:id="@+id/chip1"
27+
style="@style/Widget.MaterialComponents.Chip.Filter"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:ellipsize="end"
31+
android:text="@string/chip_text"/>
32+
<com.google.android.material.chip.Chip
33+
android:id="@+id/chip2"
34+
style="@style/Widget.MaterialComponents.Chip.Filter"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:ellipsize="end"
38+
android:text="@string/chip_text"/>
39+
<com.google.android.material.chip.Chip
40+
android:id="@+id/chip3"
2841
style="@style/Widget.MaterialComponents.Chip.Filter"
2942
android:layout_width="wrap_content"
3043
android:layout_height="wrap_content"

0 commit comments

Comments
 (0)