How to get multiple Chips selection in Android ChipGroup

In this android programming source code example, we are going to get multiple Chips selection in Android ChipGroup.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to get multiple Chips selection in Android ChipGroup.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="15dp"
    android:background="#CACACA">

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/gender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:singleSelection="false">
        <com.google.android.material.chip.Chip
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginLeft="20dp"
            android:text="Male"
            app:checkedIconEnabled="true"
            style="@style/Widget.MaterialComponents.Chip.Filter"/>

        <com.google.android.material.chip.Chip
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Female"
            app:checkedIconEnabled="true"
            style="@style/Widget.MaterialComponents.Chip.Filter"/>

        <com.google.android.material.chip.Chip
            android:id="@+id/both"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Both"
            android:textSize="20sp"
            app:checkedIconEnabled="true"
            style="@style/Widget.MaterialComponents.Chip.Filter"/>
    </com.google.android.material.chip.ChipGroup>

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;

import java.util.ArrayList;

public class AndroidChipActivity11 extends AppCompatActivity {
    private ChipGroup chipGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_chip11);
        chipGroup = (ChipGroup)findViewById(R.id.gender);
        final ArrayList<String> list = new ArrayList<>();
        for(int i = 0; i < chipGroup.getChildCount(); i++){
            Chip chip = (Chip) chipGroup.getChildAt(i);
            chip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        list.add(buttonView.getText().toString());
                    }else{
                        list.remove(buttonView.getText().toString());
                    }
                    if(!list.isEmpty()){
                        Toast.makeText(AndroidChipActivity11.this, list.toString(),Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }
}

values/styles.xml

<style name="ChipTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

app/build.gradle

implementation 'com.google.android.material:material:1.0.0'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment