Android Filter Chip Using Kotlin With Example

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

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

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

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

Output

Tutorialwing Kotlin Android Filter Chip Using Kotlin With Example

Getting Started

We can define android Filter Chip widget as below –

Filter Chips are used to display filter for a collection. It is a good collection alternative to toggle button or checkboxes.

Now, how do we use Filter Chip 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 ChipFilter. 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 Filter Chip 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 Filter Chip in Kotlin

Follow steps below to use Filter Chip in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ChipFilter</string>
        <string name="choose_amenities">Choose Amenities</string>
        <string name="elevator">Elevator</string>
        <string name="washer_dryer">Washer / Dryer</string>
        <string name="fireplace">Fireplace</string>
        <string name="wheelchair_access">Wheelchair Access</string>
        <string name="dogs_ok">Dogs OK</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"
        android:padding="10dp"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:text="@string/choose_amenities"
            android:textSize="16sp"
            android:textStyle="bold"
            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_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label"
            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/elevator" />
    
            <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/washer_dryer" />
    
            <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/fireplace" />
    
            <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/wheelchair_access" />
    
            <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/dogs_ok" />
    
        </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.chipfilter
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.tutorialwing.chipfilter.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)
    	}
    }
    

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

Different Attributes of Filter Chip in XML

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

Set Id of Filter Chip

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 Filter Chip using android:id attribute like below –

    <com.google.android.material.chip.Chip
        android:id="@+id/filterChip_ID"
        />

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

Set Width of Filter Chip

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

    <com.google.android.material.chip.Chip
        android:id="@+id/filterChip_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 Filter Chip Dynamically

Set Height of Filter Chip

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

    <com.google.android.material.chip.Chip
        android:id="@+id/filterChip_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 Filter Chip Dynamically

Set Padding of Filter Chip

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

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

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

Set Margin of Filter Chip

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

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

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

Set Background of Filter Chip

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

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

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

Set Visibility of Filter Chip

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

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

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

Different Attributes of Android Filter Chip Widget

Learn about different attributes of filter chip.

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

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

Leave a Reply