ChipGroup Using Kotlin With Example

In this article, we will learn about android ChipGroup using Kotlin. We will go through various example that demonstrates how to use different attributes of ChipGroup. For example,

In this article, we will get answer to questions like –

  • What is ChipGroup?
  • Why should we consider ChipGroup while designing ui for any app?
  • What are possibilities using ChipGroup while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Android Kotlin ChipGroup Using Kotlin With Example

Getting Started

We can define android ChipGroup widget as below –

A ChipGroup is used to hold multiple chips. By default, chips are arranged in multiple lines. However, we can align it in single line too. Chips can be selected as well. If we want to select only one chip at a time, it is also possible.

Now, how do we use ChipGroup in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as ChipGroup. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android ChipGroup Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using ChipGroup in Kotlin

Follow steps below to use ChipGroup in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ChipGroup</string>
        <string name="india">India</string>
        <string name="biking">Biking</string>
        <string name="aeroplane">Aeroplane</string>
        <string name="mobile">Mobile</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/labelMultipleSelection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:paddingStart="10dp"
            android:text="Multiple Selection"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/labelMultipleSelection"
            app:singleLine="false">
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip1"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/india" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip2"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/biking" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip3"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/aeroplane" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip4"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mobile" />
    
        </com.google.android.material.chip.ChipGroup>
    
        <TextView
            android:id="@+id/labelSingleSelection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="10dp"
            android:paddingStart="10dp"
            android:text="Single Selection"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/chipGroup" />
    
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/labelSingleSelection"
            app:singleLine="false"
            app:singleSelection="true">
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip5"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/india" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip6"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/biking" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip7"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/aeroplane" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip8"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mobile" />
    
        </com.google.android.material.chip.ChipGroup>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.chipgroup
    
    import android.os.Bundle
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import com.tutorialwing.chipgroup.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		setContentView(binding.root)
    
    		setupChipGroup()
    	}
    
    	private fun setupChipGroup() {
    		binding.chip1.setOnCloseIconClickListener {
    			binding.chipGroup.removeView(it)
    			Toast
    				.makeText(this, "Removed 1st Chip", Toast.LENGTH_SHORT)
    				.show()
    		}
    
    		binding.chip2.setOnCloseIconClickListener {
    			binding.chipGroup.removeView(it)
    			Toast
    				.makeText(this, "Removed 2nd Chip", Toast.LENGTH_SHORT)
    				.show()
    		}
    		binding.chip3.setOnCloseIconClickListener {
    			binding.chipGroup.removeView(it)
    			Toast
    				.makeText(this, "Removed 3rd Chip", Toast.LENGTH_SHORT)
    				.show()
    		}
    		binding.chip4.setOnCloseIconClickListener {
    			binding.chipGroup.removeView(it)
    			Toast
    				.makeText(this, "Removed 4th Chip", Toast.LENGTH_SHORT)
    				.show()
    		}
    	}
    }
    

Now, run the application. We will get output as below –
Tutorialwing Android Kotlin Entry or Input Chip With ChipGroup Using Kotlin With Example

Now, what if we want to use ChipGroup with Filter or Choice type of chips ? How can we do it ?

Here, we have covered it in separate section. Please continue reading….

Using Filter Chips with ChipGroup in Kotlin

  • Create a new xml file, activity_main_filter.xml, in res/layout folder. Then, add below code into it –
    <?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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/labelMultipleSelection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:paddingStart="10dp"
            android:text="Multiple Selection"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/labelMultipleSelection"
            app:singleLine="false">
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip1"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/india" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip2"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/biking" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip3"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/aeroplane" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip4"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mobile" />
    
        </com.google.android.material.chip.ChipGroup>
    
        <TextView
            android:id="@+id/labelSingleSelection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="10dp"
            android:paddingStart="10dp"
            android:text="Single Selection"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/chipGroup" />
    
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/labelSingleSelection"
            app:singleLine="false"
            app:singleSelection="true">
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip5"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/india" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip6"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/biking" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip7"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/aeroplane" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip8"
                style="@style/Widget.MaterialComponents.Chip.Filter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mobile" />
    
        </com.google.android.material.chip.ChipGroup>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • Now, copy and paste below code in MainActivity.kt file.

    package com.tutorialwing.chipgroup
    
    import android.os.Bundle
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import com.tutorialwing.chipgroup.databinding.ActivityMainFilterBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainFilterBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    
    		binding = ActivityMainFilterBinding.inflate(layoutInflater)
    		setContentView(binding.root)
    	}
    }
    
  • Now, run application. We will get output as shown below –

Tutorialwing Android Kotlin Filter Chip With ChipGroup Using Kotlin With Example

Using Choice Chips with ChipGroup in Kotlin

Now, we will use choice chips with ChipGroup. Follow steps below to do it –

  • Add new xml file, activity_main_choice.xml, in res/layout folder. Then, add below code in it –
    <?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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/labelMultipleSelection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:paddingStart="10dp"
            android:text="Multiple Selection"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/labelMultipleSelection"
            app:singleLine="false">
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip1"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/india" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip2"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/biking" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip3"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/aeroplane" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip4"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mobile" />
    
        </com.google.android.material.chip.ChipGroup>
    
        <TextView
            android:id="@+id/labelSingleSelection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="10dp"
            android:paddingStart="10dp"
            android:text="Single Selection"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/chipGroup" />
    
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/labelSingleSelection"
            app:singleLine="false"
            app:singleSelection="true">
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip5"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/india" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip6"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/biking" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip7"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/aeroplane" />
    
            <com.google.android.material.chip.Chip
                android:id="@+id/chip8"
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mobile" />
    
        </com.google.android.material.chip.ChipGroup>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • Now, we need to update out MainActivity file. so, copy and paste below code in MainActivity.kt file –
    package com.tutorialwing.chipgroup
    
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    import com.tutorialwing.chipgroup.databinding.ActivityMainChoiceBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainChoiceBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    
    		binding = ActivityMainChoiceBinding.inflate(layoutInflater)
    		setContentView(binding.root)
    	}
    }
    
  • Now, run application. We will get output as shown below –
    Tutorialwing Android Kotlin Choice Chip With ChipGroup Using Kotlin With Example

Different Attributes of ChipGroup in XML

Now, we will see how to use different attributes of Android ChipGroup using Kotlin to customise it –

Set Id of ChipGroup

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of ChipGroup using android:id attribute like below –

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup_ID"
        />

Here, we have set id of ChipGroup as chipGroup_ID using android:id=”” attribute. So, if we need to reference this ChipGroup, we need to use this id – chipGroup_ID.
Learn to Set ID of ChipGroup Dynamically

Set Width of ChipGroup

We use android:layout_width=”” attribute to set width of ChipGroup.
We can do it as below –

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of ChipGroup Dynamically

Set Height of ChipGroup

We use android:layout_height=”” attribute to set height of ChipGroup.
We can do it as below –

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of ChipGroup Dynamically

Set Padding of ChipGroup

We use android:padding=”” attribute to set padding of ChipGroup.
We can do it as below –

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in ChipGroup using android:padding=”” attribute.
Learn to Set Padding of ChipGroup Dynamically

Set Margin of ChipGroup

We use android:layout_margin=”” attribute to set margin of ChipGroup.
We can do it as below –

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in ChipGroup using android:layout_margin=”” attribute.
Learn to Set Margin of ChipGroup Dynamically

Set Background of ChipGroup

We use android:background=”” attribute to set background of ChipGroup.
We can do it as below –

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in ChipGroup using android:background=”” attribute.
Learn to Set Background of ChipGroup Dynamically

Set Visibility of ChipGroup

We use android:visibility=”” attribute to set visibility of ChipGroup.
We can do it as below –

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of ChipGroup using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of ChipGroup Dynamically

Different Attributes of Android ChipGroup Widget

Below are the various attributes that are used to customise android ChipGroup Widget. However, you can check the complete list of attributes of ChipGroup in it’s official documentation site. Here, we are going to list some of the important attributes of this widget inherited from view –

Sr. XML Attributes Description
1 android:alpha Defines alpha to the view
2 android:background Defines drawable to the background
3 android:backgroundTint Defines tint to apply to the background
4 android:clickable Defines whether the view is clickable or not
5 android:elevation Defines elevation of the view
6 android:focusable Defines whether this view can take focus or not
7 android:id Defines id of the view
8 android:visibility Defines the visibility(VISIBLE, INVISIBLE, GONE) of the view

We have seen different attributes of ChipGroup and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ChipGroup, how can we use android ChipGroup using Kotlin ? etc. We also went through different attributes of android ChipGroup.

Leave a Reply