MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.widget.TextView
import androidx.core.view.children
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
class MainActivity : Activity() {
private lateinit var chipGroup:ChipGroup
private lateinit var textView:TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
chipGroup = findViewById(R.id.chipGroup)
textView = findViewById(R.id.textView)
// get chip group initially checked chips
handleSelection()
// set checked change listener for each chip on chip group
chipGroup.children.forEach {
(it as Chip).setOnCheckedChangeListener {
buttonView, isChecked ->
handleSelection()
}
}
}
// function to get chip group checked chips
private fun handleSelection(){
textView.text = "Checked Chips : "
chipGroup.checkedChipIds.forEach{
val chip = findViewById<Chip>(it)
textView.append("\n${chip.text}")
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="#DCDCDC"
android:padding="32dp">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:singleSelection="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.chip.Chip
android:id="@+id/chipRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:text="Red" />
<com.google.android.material.chip.Chip
android:id="@+id/chipGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:checked="true"
android:text="Green" />
<com.google.android.material.chip.Chip
android:id="@+id/chipYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:text="Yellow" />
<com.google.android.material.chip.Chip
android:id="@+id/chipBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:checked="true"
android:text="Blue" />
<com.google.android.material.chip.Chip
android:id="@+id/chipBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:text="Black" />
</com.google.android.material.chip.ChipGroup>
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-condensed-medium"
android:gravity="center"
android:padding="8dp"
android:textColor="#4F42B5"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chipGroup"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrtCxGxGx6zGpkOu1zK_Cl4zIm9oGUd1OGBaHk4nGybwZo0ogOK7rz4TN7pSoyQJuw39DHXGsuSbVPi78NwUXe1O7iu6-_2LpNeqEx_9o39dVbo-jRrYdyPaEqes0BUibcwk9XNG5xBN7FZez4yay-bxh-tt01jtys7_HPNCa15hXfDJDUz9Uwgnbg8Q/s1600/android_kotlin_chipgroup_get_selected_chips.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKviiR0SLa_9BSk_b-o-MHsLGKng7ozp_ExjaIy39SwIb9xiSHOIG3X7Q0V6EvSJ_efwosV4qxsssuNJ5TzyXIZWMKK7O_qbrfizuCcW0cXFZ2JBi4RoslvBMUv4D8inW3wK4kje-hv0aRiroHqmjN_ADpoieyxcuNuFSx4NoGRTbGP_uORRvHd623NQ/s1600/android_kotlin_chipgroup_get_selected_chips2.png)
- android kotlin - Chip border color
- android kotlin - Chip background color
- android kotlin - Chip checked color programmatically
- android kotlin - ChipGroup add chip programmatically
- android kotlin - Get screen width and height in dp
- android kotlin - Get screen density programmatically
- android kotlin - Convert pixels to dp programmatically
- android kotlin - Get screen size programmatically
- android kotlin - RecyclerView animation
- android kotlin - RecyclerView smooth scroll
- android kotlin - RecyclerView add remove item
- android kotlin - RecyclerView GridLayoutManager example
- android kotlin - RecyclerView StaggeredGridLayoutManager example
- android kotlin - Border/divider between GridView items
- android kotlin - GridView OnItemClickListener