3

I want to handle multiple chips selection but Chipgroup.setOnCheckedChangedListener(); method works if i add app:singleSelection = "true" and if i do that so i cannot select multiple chips. I don't understand how i select multiple chips from chipgroup.

MainActivity.java

 private ChipGroup chipGroup;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                chipGroup = findViewById(R.id.chipg);

                for (int i = 0; i < 10; i++) {
                    ChipMaking(String.valueOf(i));
                }

                chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(ChipGroup chipGroup, @IdRes int i) {
                        Chip chip = chipGroup.findViewById(i);
                        Toast.makeText(getApplicationContext(), "Chip is " + chip.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                });

            }

            public void ChipMaking(String tag) {
                Chip chip = new Chip(this);
                chip.setId(Integer.parseInt(tag));
                chip.setText(tag);
                chip.setTextAppearanceResource(R.style.ChipTextStyle);
                chip.setPaddingRelative(5, 5, 5, 5);
                chip.setElevation(5);
                chip.setCheckable(true);
                chip.setClickable(true);
                chipGroup.addView(chip);
            }

MainActivity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp">

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/chipg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_marginTop="20dp"
                android:foregroundGravity="center">
            </com.google.android.material.chip.ChipGroup>
        </ScrollView>

        <!--app:singleSelection="true"-->
    </RelativeLayout>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Himanshu
  • 79
  • 1
  • 7

1 Answers1

4

I add new Chip with setOnCheckedChangeListener programmatically and use private int for check the number of chip selected.

View newChip = LayoutInflater.from(this).inflate(R.layout.chip, null);
((Chip) newChip).setOnCheckedChangeListener(changeListener);
chipGroup.addView(newChip);

and

private CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            chipNumber = chipNumber + 1;
        } else {
            if (chipNumber > 0) {
                chipNumber = chipNumber - 1;
            }
        }
    }
};

if (chipNumber > 0) may not be necessary. Seems like this, you may control the selected chips.

This can also use ChipGroup's singleSelection="false"

HJ Song
  • 41
  • 1
  • 5