A customizable Android view based on ChipGroup, designed to limit rows of chips and add a toggle chip for showing or hiding additional content.
- Row limitation: Restrict the number of visible chip rows to improve layout management.
- Expandable toggle chip: Optionally adds a chip for expanding or collapsing, depending on the current state. The toggle chip dynamically adjusts to reflect the number of hidden chips or can be omitted entirely if not needed.
- Responsiveness: Automatically calculates the size of the ChipGroup to properly arrange the chips based on the available space.
- Easy integration: Built on
ChipGroup, leveraging its familiar API.
- Minimum SDK version: Android 8.0 (API level 26).
To integrate ExpandableRowChipGroup into your project, add the following to your build.gradle files:
1. Add JitPack repository
repositories {
maven("https://jitpack.io")
}
2. Add the dependency
dependencies {
implementation("com.github.Tefoxx:ExpandableRowChipGroup:1.0.8")
}
ExpandableRowChipGroup provides the following custom attributes:
| Attribute | Description | Default Value |
|---|---|---|
app:expandChipLayout |
Layout resource for the "expand" chip (e.g., "More"). | nothing |
app:hideChipLayout |
Layout resource for the "hide" chip (e.g., "Hide"). | nothing |
app:maxRows |
Maximum number of visible rows | Int.MAX_VALUE |
ExpandableRowChipGroup provides the following methods for managing chips and their behavior:
| Method | Description |
|---|---|
setChips(chips: List<Chip>) |
Dynamically sets the list of chips to display. Clears any previously added chips. |
setControlButtonText(onControlButtonAppear: ((state: State, hiddenChips: Int) -> String)?) |
Customizes the text of the control button ("More N+" / "Hide") dynamically based on the state and hidden chips. |
getCheckedChip(): Chip? |
Returns the currently checked chip, if any, or null if none are selected. |
<com.ent21.expandablerowchipgroup.ExpandableRowChipGroup
android:id="@+id/expandableRowChipGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:chipSpacingHorizontal="8dp"
app:chipSpacingVertical="8dp"
app:expandChipLayout="@layout/expand_chip"
app:hideChipLayout="@layout/hide_chip"
app:maxRows="2" />
expand_chip.xml
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:textColor="#FFFFFFe"
app:chipBackgroundColor="#000000"
app:chipCornerRadius="16dp"
app:chipStrokeWidth="0dp"
app:chipSurfaceColor="@android:color/transparent" />
hide_chip.xml
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide"
android:textColor="#FFFFFFe"
app:chipBackgroundColor="#000000"
app:chipCornerRadius="16dp"
app:chipStrokeWidth="0dp"
app:chipSurfaceColor="@android:color/transparent" />
If you need to create dynamic chips:
val chipGroup = findViewById<ExpandableRowChipGroup>(R.id.expandableRowChipGroup)
val chips = List(16) {
val chip = inflater.inflate(
R.layout.your_custom_chip,
expandableChipGroup,
false
) as Chip
chip.text = "Chip #$it"
chip
}
expandableChipGroup.setChips(chips)
If you need to set the text for control chips:
val chipGroup = findViewById<ExpandableRowChipGroup>(R.id.expandableRowChipGroup)
chipGroup.setControlButtonText { state, hiddenChips ->
when (state) {
ExpandableRowChipGroup.State.EXPANDED -> "Hide"
ExpandableRowChipGroup.State.COLLAPSED -> "Show ($hiddenChips)"
}
}
Check out how ExpandableRowChipGroup works:
This library is distributed under the MIT License. Feel free to use it in your projects.
