MainActivity.kt
package com.cfsuman.jetpack
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import com.google.android.material.chip.Chip
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Create an empty list
val list = mutableListOf<String>()
// Loop through the chips
for (index in 0 until chipGroup.childCount) {
val chip:Chip = chipGroup.getChildAt(index) as Chip
// Set the chip checked change listener
chip.setOnCheckedChangeListener{view, isChecked ->
if (isChecked){
list.add(view.text.toString())
}else{
list.remove(view.text.toString())
}
if (list.isNotEmpty()){
// SHow the selection
toast("Selected $list")
}
}
}
}
}
// Toast extension method
fun Context.toast(message:String)=
Toast.makeText(this,message,Toast.LENGTH_SHORT).show()
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
tools:context=".MainActivity"
android:background="#fdfdfc">
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/chipGroup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="32dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:background="#fafff7"
app:singleLine="false"
app:singleSelection="false">
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chipRed"
android:text="Red"
style="@style/Widget.MaterialComponents.Chip.Filter"
app:checkedIconEnabled="true"
/>
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chipGreen"
android:text="Green"
style="@style/Widget.MaterialComponents.Chip.Filter"
app:checkedIconEnabled="true"
/>
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chipBlue"
android:text="Blue"
style="@style/Widget.MaterialComponents.Chip.Filter"
app:checkedIconEnabled="true"
/>
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chipYellow"
android:text="Yellow"
style="@style/Widget.MaterialComponents.Chip.Filter"
app:checkedIconEnabled="true"
/>
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chipPink"
android:text="Pink"
style="@style/Widget.MaterialComponents.Chip.Filter"
app:checkedIconEnabled="true"
/>
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chipWhite"
android:text="White"
style="@style/Widget.MaterialComponents.Chip.Filter"
app:checkedIconEnabled="true"
/>
</com.google.android.material.chip.ChipGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
res/values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
gradle dependencies
// Material components theme
implementation 'com.google.android.material:material:1.0.0'