Android Chips Example: Material Design


Android Chips Example Featured

Google’s material design has been evolving year by year. At first when Material Design was introduced a lot of new components were listed in its documentation. But their implementation or official widgets were not released at that time. But with every support library release, few new significant components are released. With this year’s Google I/O, Androidx was released as a new implementation for Android’s support library framework, along with this a new material design component library was also released. As I mentioned above a few new components were released this year too, one of them is Android Chip. This new Android chips widget is now a part of design support library v28 and has also been introduced in this new material design component library 1.0.0. This library has many components but in this article we will be discussing on Android chips only and how to make an Android chips example, via the new Material Component library.

Android Pill button or chips have always been a part of our apps. One way or the other building a button like a pill has never been a difficult task for us. But now that Google has officially released a widget by the name of Chips, it will definitely make our lives easier to some level. In this Android chips example we would learn how to make an android chip button which looks like a pill shaped interact-able element. Please note: To make use of this example, you might have to switch over to AndroidX and com.google.android.material packages by refactoring from the Refactor menu of the Android Studio, as the new material library does not work with old support packages. If you wish to use old support libraries for the time being, please use the v28 of it. It has the Chip widget.

 

Exploring Material Design : Android Chips Example

Generally Android chips are used to capture user input, filter content, trigger actions and simply to make a selection from a choice of options. It should not be used in a situation where a single call to action has to be performed. One of the most obvious example of Android Chips, that you must have seen, should be the Gmail app. In Gmail app when an email address is entered, it is shown in an Android Chip like view. If you wish to make a UI element which looks like the gmail email address pill button type view, you should use the Android Chip widget. To start with the implementation part, first you would have to include the following dependency in your gradle file:

 

Using Android Chips with Styles

In simple terms Android Chip is just a combined object of text, icon and optional close icon. With this, the new material design component also gives us a few new attributes to change its appearance and behavior. It allows us to make it represent an attribute, text, entity, or perform an action. This eventually allows the user to trigger an action, filter content, select a choice, or enter information. Also if you have to represent a contact with image, this Android chip can be of good use. To have a more detailed look at the chips, lets have a look at its styles.

 

Action Chip (Default)

The most basic chip of all the available ones is the Action chip. Its a single actionable chip, in short its an alternative to the button widget. If you have to perform an action, based on users tap, this is the chip that you should use. Generally this type of chip is placed after the content of the page and is used as a clickable, just like a button. Have a look at the images below to understand its usage.

To make a working example of Android action chip, you can either define it in the XML or you can insert it dynamically by Java/Kotlin code. In this Android chips example, we will do so by XML and will use its properties in Java code. Please have a look below:

In the above code, please see that we have defined a style tag style="@style/Widget.MaterialComponents.Chip.Action". This is used to define the style for action chip, but in case if this is missing, it would still render an Android chip with ‘Action’ style. As this is also the default style. Moving ahead, to capture this Android chip’s click event, we use this piece of code:

 

Entry Chip (Input Chip)

Another type of chip is the input one, this chip used display captured user input in a non editable format. Its a very nice way to showcase the user input. One of the most obvious usages of Android Entry chip is shown in the gmail app. Particularly the email input experience- when an email is entered by the user, it is showcased as an entry chip, sort of a pill shaped layout. Now according to the material design this element should be an Android Chip with style as ‘Entry’. This chip can also have a close icon along with a chip icon. Generally this type of chip is contained in a ChipGroup. ChipGoup class is similar to RadioGroup class, as it is used to hold multiple chips. It has the properties to display all the chips in a single line. Have a look at the images below to have a better understanding on how the entry chips would look:Android Chips Example - Entry Chip

In case of Android entry chips, in the XML we will create an empty ChipGroup and add chips programmatically. Lets have a look at the layout.xml of the activity:

Now since we will be adding chips dynamically through code, a big challenge would be to maintain style along the chips. As per my understanding to maintain consistent styles there are two ways, either by code we style it and use in a loop. Or we create a standard chip style in XML and use it across. Interestingly Android Chips widget also supports a ChipDrawable class through which we can create a chip dynamically, and this ChipDrawable object can be created by using an XML resource. Therefore as per the principle I like this way better, have a look at my chip style which I used to inject a chip in ChipGroup :

Next to create an Input Chip dynamically we need to define a method :

And this is how an Android Entry Chip is inserted in the chip group:

 

Filter Chip

These type of chips should be used in situations exactly as its name suggests, i.e. for creating filters. Android filter chips are similar to check boxes, in a way they should be used to filter the content. For example if you have to show a UI where there’s a list and user has to filter a specific type of content from it, in this situation a filter chip would be an apt solution. Generally filter chips do not have a icon attached to them, as a check icon is displayed once a chip is selected. Please have a look at the images below:

Android chips example -4

Further lets define the XML for Android Filter Chips, they have style="@style/Widget.MaterialComponents.Chip.Filter" :

Now that we have filter chips in place, we need to detect clicks on the filter chips. We can do so simply by attaching a OnCheckedChangeListener to it:

 

Filter Chip – Single Selection Mode

In this Android Chips Example, I will also show a filter chip with single selection mode. In this mode, filter chips would start acting as a radio button. This means in a way only one selection can be made at a time. This functionality is achieved by adding all the filter chips in a ChipGroup and setting the property : app:singleSelection="true" of Chip Group. To have a better understanding, lets have a look at the layout XML file:

Next to capture the selected chip out of all chips, we need to attach a ChipGroup.OnCheckedChangeListener() on the ChipGroup.

Please Note: This ChipGroup.OnCheckedChangeListener() would return callbacks only when single selection mode is set to true.

 

Choice Chip

A perfect example of choice chip is similar to the filter chip one. Choice chip allows the user to select only one of the available options just like the filter chips. But with one difference, it does not have a check icon on it. Instead according to material design guidelines, it displays the selection with a change of color, when it is selected. If you wish you can use a chipIcon with this chip. But ideally they should be used wherever you feel the need to show RadioButton. Lets have a look at the UI for Android Choice Chip:

Android chips example -5

 

To use the choice chip, we use style="@style/Widget.MaterialComponents.Chip.Choice" parameter in the XML. Lets have a look at the XML:

As shown in this Android Chips Example earlier, ChipGroup can return the selected chip only when single selection mode is set to true. Even here we will utilize the same property and capture the selection:

If you wish to have a look at all the working sample of the Android Chip types mentioned above, please have a look at the full source code here: 

Full Source Code 

In this Android Chips Example, as you can see we built a working sample of all the chip styles, by using the latest Google’s material library. Here in this tutorial we also showcased, that a chip has the properties of a radio button as well as a check box. In a way its a better representation of both. You can use it either in a single selection or in multiple selection mode. Both ways it would give a great experience. As the Android Chips are compact, relevant and focused. Hope this example made you understand things better, for latest news on Android connect with us on Twitter, Facebook and Google+. Also subscribe to our newsletter, for news right in your inbox.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

Your email address will not be published. Required fields are marked *