Material Design Components Chips in Android with Example Last Updated : 06 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Chips in android are one of the components which are used to make the choice filters, actions, and display the selectable options in the compact area of the Android Window. In this article, it's been discussed how to implement the very basic selectable chips for the filtering of the options. Have a look at the following image to get an idea of what's been discussed further. Note that we are going to implement this project using the Java language. Steps to Implement the Selectable Chips in AndroidStep 1: Create an empty projectCreate an empty activity Android Studio project. Refer to Android | How to Create/Start a New Project in Android Studio? to know how to create an empty activity Android Studio project.Note that select Java as the programming language.Step 2: Adding the dependency to the app-level gradle fileThe current project needs Material Design Dependency.Invoke the following dependency inside the app-level gradle file.Make sure the system is connected to the network so that Android Studio can download the required files.And click on the "Sync Now" button which appears on the top right corner.implementation 'com.google.android.material:material:1.3.0-alpha03'Step 3: Working with the activity_main.xml fileThe tag required to implement the basic chip in Android is as follows:com.google.android.material.chip.ChipTo implement the same in the layout invoke the following code. This layout includes a very basic chip from Material Design Dependency. XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" tools:ignore="HardcodedText"> <com.google.android.material.chip.Chip android:id="@+id/chipCpp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="64dp" android:text="GEEKS FOR GEEKS" /> </RelativeLayout> Output UI:Step 4: Handling Chip in MainActivity.java fileThe Material design chip button is handled using "OnClickListener", as same as that of the normal Button.Invoke the following code inside the MainaActivity.java file to handle the chip button, which shows the simple Toast message. Java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.google.android.material.chip.Chip; public class MainActivity extends AppCompatActivity { Chip chip1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // chips is handled using the // normal OnClickListener callback chip1 = findViewById(R.id.chipGfg); chip1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Action Completed", Toast.LENGTH_SHORT).show(); } }); } } Output: Run on EmulatorMaking the Chips SelectableTo make the chip selectable in android the chip group needs to implement instead of a single chip.This helps in filtering the selections of the user choices.To implement the chip group in android invoke the following code inside the activity_main.xml file. XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" tools:ignore="HardcodedText"> <!--make sure to group the chips--> <!--style attribute is mandatory for each of the chips--> <com.google.android.material.chip.ChipGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"> <com.google.android.material.chip.Chip android:id="@+id/chipCpp" style="@style/Widget.MaterialComponents.Chip.Filter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C++" /> <com.google.android.material.chip.Chip android:id="@+id/chipJava" style="@style/Widget.MaterialComponents.Chip.Filter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="JAVA" /> <com.google.android.material.chip.Chip android:id="@+id/chipPython" style="@style/Widget.MaterialComponents.Chip.Filter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" /> </com.google.android.material.chip.ChipGroup> </RelativeLayout> To handle the selection of the chips whether the chips are filtered by the user or not, invoke the following code inside the MainActivity.java file. Java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.google.android.material.chip.Chip; public class MainActivity extends AppCompatActivity { Chip chipCpp, chipJava, chipPython; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chipCpp = findViewById(R.id.chipCpp); chipJava = findViewById(R.id.chipJava); chipPython = findViewById(R.id.chipPython); chipCpp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // check whether the chips is filtered by user // or not and give the suitable Toast message if (chipCpp.isChecked()) { Toast.makeText(MainActivity.this, "C++ selected", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "C++ deselected", Toast.LENGTH_SHORT).show(); } } }); chipJava.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // check whether the chips is filtered by user or // not and give the suitable Toast message if (chipJava.isChecked()) { Toast.makeText(MainActivity.this, "JAVA selected", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "JAVA deselected", Toast.LENGTH_SHORT).show(); } } }); chipPython.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // check whether the chips is filtered by user or // not and give the suitable Toast message if (chipPython.isChecked()) { Toast.makeText(MainActivity.this, "Python selected", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Python deselected", Toast.LENGTH_SHORT).show(); } } }); } } Output: Run on Emulator Comment More infoAdvertise with us Next Article Material Design Components Chips in Android with Example adityamshidlyali Follow Improve Article Tags : Java Technical Scripter Android Technical Scripter 2020 Practice Tags : Java Similar Reads Material Design Buttons in Android with Example Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro 6 min read Theming Material Design Buttons in Android with Examples Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android application. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Androi 7 min read Material Design Components Sliders in Android Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro 4 min read Snackbar Material Design Components in Android The various other Material design components need special attributes to get implemented. But in this article, the Material design Snackbar is implemented and it doesn't need the special attributes to get implemented in the application. Have a look at the following image to differentiate between the 8 min read Material Design Component Navigation Rail in Android Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro 4 min read Material Design Component Bottom App Bar in Android A Bottom App Bar is a UI element typically located at the bottom of a mobile screen. It works like a container for navigation buttons, action icons, and sometimes floating action buttons (FABs). Unlike a top app bar, the bottom placement makes it easier to reach, especially on larger screens. In thi 4 min read Material Design Components Progress Indicator in Android Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro 4 min read Theming Material Design Snackbars in Android with Example In the previous article, it's been discussed Snackbar Material Design Components in Android. In this article, it's been discussed how we can theme material design Snackbar and increase the User Experience. Theming Example 1:This method is done using the styles.xml file. Where we need to override the 5 min read Theming of Material Design EditText in Android with Example Prerequisite: Material Design EditText in Android with Examples In the previous article Material Design EditText in Android with Examples Material design Text Fields offers more customization and makes the user experience better than the normal text fields. For example, Icon to clear the entire Edit 4 min read Scratch Card View in Android with Example Scratch Card View is one of the most seen UI components in Android apps. This type of UI component is generally seen in payment apps such as Google Pay and many other payment apps. Now if you are an Android developer then you should get amazed at How we can create this type of UI component in our An 3 min read Like