12

I search a lot on internet but couldn't find the exact solution. Here is the link that i last tried from SO. Get selected Chips from a ChipGroup

I want to get selected chips from chip group when a button is clicked.

This function contain information of displaying names in RecyclerView

private void getNames() {
    List<String> names = Arrays.asList(getResources().getStringArray(R.array.names));
    int count = 0;
    for ( String name : names){
        list.add(new Names(name));
        count++;
    }
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    namesAdapter = new NamesAdapter(MainActivity.this, list);
    recyclerView.setAdapter(namesAdapter);
}

When click on RecyclerView item one chip is added into the ChipGroup here is the function

public void onItemSelected(Names name) {
    Chip chip = new Chip(this);
    chip.setText(name.getName());
    chip.setCloseIconVisible(true);
    chip.setCheckable(false);
    chip.setClickable(false);
    chip.setOnCloseIconClickListener(this);
    chipGroup.addView(chip);
    chipGroup.setVisibility(View.VISIBLE);
}

This is the image of displaying chips in ChipGroup

Function that is getting values from ChipGroup

public void getChipGroupValues(){
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ChipGroup chipGroup = findViewById(R.id.chipGroup);
            for (int i=0; i<chipGroup.getChildCount();i++){
                Chip chip = (Chip)chipGroup.getChildAt(i);
                Log.i("outside if ", i+ " chip = " + chip.getText().toString());
                if (chip.isChecked()){
                    Log.i("inside if ", i+ " chip = " + chip.getText().toString());
                    textView.setText(chip.getText().toString());
                }
            }
        }
    });
}

This is output enter image description here

build.gradle(Module.app) detail enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
msayubi76
  • 1,446
  • 1
  • 12
  • 18

5 Answers5

30

Starting with the version 1.2.0 you can use the method chipGroup.getCheckedChipIds()

List<Integer> ids = chipGroup.getCheckedChipIds();
for (Integer id:ids){
      Chip chip = chipGroup.findViewById(id);
      //....
}

OLD ANSWER with 1.1.0:

currently there isn't a direct API to get the selected chips.
However you can iterate through the children of the ChipGroup and check chip.isChecked().

   ChipGroup chipGroup = findViewById(R.id.....);
   for (int i=0; i<chipGroup.getChildCount();i++){
      Chip chip = (Chip)chipGroup.getChildAt(i);
      if (chip.isChecked()){
         //this chip is selected..... 
      }
    }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • A little close this is working fine but outsite the the "if" statement i'm getting all values not insite the "if" condition. what is the purpose of this statement? – msayubi76 Oct 04 '19 at 02:45
  • @msayubi76 You question is *I want to get selected chips from chip group when a button is clicked*. In your `OnClickListener` you have to iterate through the `ChipGroup` and get only the selected values. The `if` statement is the only way to know if each `chip` is `checked` – Gabriele Mariotti Oct 04 '19 at 05:46
  • but in this case control is not going inside the if statement.i'm getting values outside the if statement – msayubi76 Oct 04 '19 at 19:07
  • @msayubi76 which version are you using of material components library? I've just tried the code and it works. – Gabriele Mariotti Oct 04 '19 at 19:10
  • i added this in dependencies "com.google.android.material:material:1.0.0" – msayubi76 Oct 05 '19 at 08:31
  • it is the latest stable and at the same time it was release quite 1 year ago. try the 1.1.0-alpha10 (it will become beta soon) – Gabriele Mariotti Oct 05 '19 at 08:33
  • i added 1.1.0-alpha 10 but results the same as before. also i updated the question for more detail – msayubi76 Oct 05 '19 at 14:28
  • @msayubi76 it seems that your chips are not selectable (and then no one is checked). Use `chip.setCheckable(true);` in your code and remove `chip.setClickable(false);` – Gabriele Mariotti Oct 05 '19 at 16:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200450/discussion-between-msayubi76-and-gabriele-mariotti). – msayubi76 Oct 05 '19 at 18:05
  • Helpful... Thanks – Pooja Aug 24 '20 at 19:45
  • Will this work even when you added chip to the group during runtime? What will be their id? – Bitwise DEVS Apr 25 '21 at 09:36
  • I think adding tag can be a good alternative for runtime GroupChip inflation – Bitwise DEVS Apr 25 '21 at 09:51
11

the solution I used in Kotlin with data binding

mBinding?.chipGroup?.children
            ?.toList()
            ?.filter { (it as Chip).isChecked }
            ?.forEach { //here are your selected chips as 'it' }

And here is how I got just titles

mBinding?.chipGroup?.children
            ?.toList()
            ?.filter { (it as Chip).isChecked }
            ?.joinToString(", ") { (it as Chip).text }
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
3

Get selected chip's text in a chip group

To get selected chip's text from chipgroup, you can use this one liner in Kotlin:

val selectedChipText = parentChipGroup.findViewById<Chip>(parentChipGroup.checkedChipId).text.toString()
Damercy
  • 939
  • 8
  • 10
  • 1
    Or if you have populated the tag variable you could use that, like: `siteTypeId = registerCardChipGroupSiteTypes .findViewById(registerCardChipGroupSiteTypes.checkedChipId).tag.toString().toLong()` – Roar Grønmo Apr 04 '21 at 08:04
2

I used below mentioned method to detect the selected chips in chipGroup.

for (chipTitle in stringList) {
    val chip = Chip(chipGroup.context)
    chip.text = chipTitle
    chip.tag = chipTitle
    chip.isClickable = true
    chip.isCheckable = true
    chip.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            Log.e(TAG, chipTitle)
        }
    }
    chipGroup.addView(chip)
}
chipGroup.isSingleSelection = true
Dilanka Laksiri
  • 408
  • 3
  • 12
1
chip_group.setOnCheckedStateChangeListener(new ChipGroup.OnCheckedStateChangeListener() {
    @Override
    public void onCheckedChanged(@NonNull ChipGroup group, @NonNull List<Integer> checkedIds) {

        List<Integer> ids = group.getCheckedChipIds();
        for (Integer id:ids){
            Chip chip = group.findViewById(id);
            Toast.makeText(BottomActivity.this, chip.getText(), Toast.LENGTH_SHORT).show();

        }


    }
});
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Gopal Reddy
  • 36
  • 1
  • 5