Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected Chips from a ChipGroup [duplicate]

I'm new to working with Chips in Android. I want to get the selected Chips from a ChipGroup when I click a button. Made is someway work with checking every Chip and add it to a Set, but want to make it more efficient. Somehow I didn't find an answer to my question myself.

There is also an error when I check 2 Chips and uncheck the first one I checked.

Here my code:

<com.google.android.material.chip.ChipGroup
                android:id="@+id/chipGroup"
                android:layout_width="300dp"
                android:layout_height="wrap_content"

                >

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip1"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:backgroundTint="#99FFFFFF"
                    />

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip2"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:backgroundTint="#99FFFFFF"/>


                <com.google.android.material.chip.Chip
                    android:id="@+id/chip3"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:backgroundTint="#99FFFFFF"/>

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip4"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4"
                    android:backgroundTint="#99FFFFFF"/>

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip5"
                    style="@style/Widget.MaterialComponents.Chip.Filter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="5"
                    android:backgroundTint="#99FFFFFF"/>

            </com.google.android.material.chip.ChipGroup>
Set<Integer> chipIds = new HashSet<>();

int chip1Id= 1;
int chip2Id= 2;
int chip3Id= 3;
int chip4Id= 4;
int chip5Id= 5;

chip1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip1Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip1Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip2Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip2Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip3Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip3Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip4Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip4Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });

        chip5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chipIds.add(chip5Id);
                } else {
                    for (int i : chipIds) {
                        if (i == chip5Id) {
                            chipIds.remove(i);
                        }
                    }
                }
            }
        });
like image 935
DanielDunkelbunt Avatar asked Dec 18 '22 17:12

DanielDunkelbunt


1 Answers

Here is my solution:

  1. just for test results, I´ve added a button in XML
<Button
  android:id="@+id/bShow"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/chipGroup"
  android:layout_margin="10dp"
  android:text="Show Checked"  
  android:onClick="buttonPressed"/>
  1. Java: just for the test, be sure you include:
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
  1. Java: add the following code
showResult = (Button) findViewById(R.id.bShow);
showResult.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) {
        String msg = "Chips checked are:";
        ChipGroup chg = findViewById(R.id.chipGroup);
        int chipsCount = chg.getChildCount();
        if (chipsCount == 0) {
            msg += " None!!";
        } else {
            int i = 0;
            while (i < chipsCount) {
              Chip chip = (Chip) chg.getChildAt(i);
              if (chip.isChecked() ) {
                msg += chip.getText().toString() + " " ;  
              }
              i++;
            };  
        }
        // show message
        Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
    }
});
like image 196
LuisCarrizo Avatar answered Dec 21 '22 10:12

LuisCarrizo