Best way to build using Chip
, check below some code snippet.
Add below code in your .xml
<com.google.android.material.chip.ChipGroup
android:id="@+id/filter_chip_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/filter_chip_txt">
<com.google.android.material.chip.Chip
android:id="@+id/filter_chip"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="Dogs"/>
<com.google.android.material.chip.Chip
android:id="@+id/filter_chip2"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Cats"/>
</com.google.android.material.chip.ChipGroup>
inside your .java
ChipGroup filterChipGroup = findViewById(R.id.filter_chip_group);
Chip filterChip = findViewById(R.id.filter_chip);
Chip filterChip2 = findViewById(R.id.filter_chip2);
CompoundButton.OnCheckedChangeListener filterChipListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, buttonView.getId() + "");
}
};
filterChip.setOnCheckedChangeListener(filterChipListener);
filterChip2.setOnCheckedChangeListener(filterChipListener);
I hope this will help to achive what you expect.