Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Chips in Material Design are used to represent complex entities in small blocks, such as a contact. They can be static, or they can be interactive. Interactive chips can be selectable, filterable, or actionable.
Ensure that you have the Material Components library added to your build.gradle
:
implementation 'com.google.android.material:material:1.4.0'
Here's a basic example of different types of chips:
<com.google.android.material.chip.Chip android:id="@+id/entryChip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Entry Chip" android:checkable="true" android:clickable="true" android:focusable="true" app:chipIcon="@drawable/ic_launcher_foreground"/>
<com.google.android.material.chip.ChipGroup android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" android:checkable="true" android:clickable="true" android:focusable="true"/> <com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" android:checkable="true" android:clickable="true" android:focusable="true"/> </com.google.android.material.chip.ChipGroup>
<com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Action Chip" android:clickable="true" android:focusable="true"/>
<com.google.android.material.chip.Chip android:id="@+id/filterChip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Filter Chip" app:chipIcon="@drawable/ic_launcher_foreground" android:checkable="true" android:clickable="true" android:focusable="true"/>
In Kotlin, you can handle interactions with the Chip component just like any other Android View:
val entryChip: Chip = findViewById(R.id.entryChip) entryChip.setOnCheckedChangeListener { chip, isChecked -> // Handle chip checked state change }
Chips come with various attributes that allow customization:
app:chipBackgroundColor
: Sets the chip's background color.app:chipStrokeWidth
and app:chipStrokeColor
: Define a border for the chip.app:chipIcon
: Sets an icon for the chip.app:closeIcon
: Sets an icon that will appear on the right side of the chip, typically used for removal action.app:chipSpacingHorizontal
and app:chipSpacingVertical
: Define spacing between chips when inside a ChipGroup
.And many more attributes for further customization.
Chips offer a compact way to display choice sets, filters, or actions. When using them, always keep in mind their primary purpose: to represent a small piece of information, like an attribute or an action. Don't overload chips with too much information, as they are designed for simplicity and clarity.
Using Material Components Library for Android Chips:
Chip
widget for implementing chips in Android. Include the library in your build.gradle
file.implementation 'com.google.android.material:material:1.5.0'
Creating chip layouts in Android with Material Design:
Chip
widget. Customize appearance and behavior using attributes such as android:text
, app:chipBackgroundColor
, and app:closeIcon
.<com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android" app:chipBackgroundColor="@color/colorPrimary" app:closeIcon="@drawable/ic_close" />
Android ChipGroup example with Material Design:
ChipGroup
widget to group multiple chips together. This allows for easy handling of selection and filtering.<com.google.android.material.chip.ChipGroup android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Java" /> <com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Kotlin" /> </com.google.android.material.chip.ChipGroup>
Customizing Material Design Chips in Android:
<com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Chip" android:textAppearance="@style/TextAppearance.AppCompat.Small" app:chipBackgroundColor="@color/customColor" app:closeIcon="@drawable/custom_close_icon" />
Handling chip interactions and events in Android:
setOnCheckedChangeListener
for ChipGroup
.chip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Handle chip click } }); chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(ChipGroup group, int checkedId) { // Handle chip selection in ChipGroup } });
Filtering and selecting options with Android Chips:
<com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Filter Option" app:checked="true" />
Working with different chip types in Android Material Design:
<com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choice Chip" style="@style/Widget.MaterialComponents.Chip.Choice" />
Material Design Chips XML layout examples in Android:
<com.google.android.material.chip.ChipGroup android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chip 1" /> <com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chip 2" /> </com.google.android.material.chip.ChipGroup>