Material Design Components Chips in Android with Example
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 Android
Step 1: Create an empty project
- Create 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 file
- The 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.
- Refer to the following image if unable to locate the app-level gradle file and invoke the dependency.
Step 3: Working with the activity_main.xml file
- The tag required to implement the basic chip in Android is as follows:
- To implement the same in the layout invoke the following code. This layout includes a very basic chip from Material Design Dependency.
Step 4: Handling Chip in MainActivity.java file
- The 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.
Output: Run on Emulator
Making the Chips Selectable
- To 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.
- 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.