Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically select the default Chip in a ChipGroup in Android?

TL;DR - What is the correct way to programatically select a default choice Chip that is a child of a ChipGroup in Android?

--

In my android app, I am using com.google.android.material.chip.Chip styled as @style/Widget.MaterialComponents.Chip.Choice components to represent choices of activities a user can select for a given route (think walk, bike, etc)

Because a route can have different types of activities, I insert each type as a different chip programatically into a com.google.android.material.chip.ChipGroup. I also select the default chip as being the first one inserted in the list using the following code during onViewCreated() of my fragment

 private fun setupTypeSelection(types: List<Type>) {
    types.forEach { type ->
        val chip = layoutInflater.inflate(R.layout.chip_type, viewBinding.typeChipGroup, false) as Chip

        chip.tag = type
        /* Init chip text and icon */

        chip.setOnClickListener {
            /* Update selected type */
        }

        if (currentType == null) {
            chip.isSelected = true
            currentType = type
        }

        viewBinding.typeChipGroup.addView(chip)
    }
}

Here's the layout definition of the ChipGroup, where a I set single selection, etc

chip_group_layout.xml

<com.google.android.material.chip.ChipGroup
      android:id="@+id/type_container"

      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="@dimen/margin_medium"

      app:chipSpacingHorizontal="@dimen/margin_medium"
      app:selectionRequired="true"
      app:singleLine="true"
      app:singleSelection="true" />

And here is the chip layout

chip_type.xml

<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:app="http://schemas.android.com/apk/res-auto"

     style="@style/Widget.MaterialComponents.Chip.Choice"

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"

     app:chipIconEnabled="true" />

The problem I'm facing is that the chip that was set programatically as selected chip.isSelected = true stays selected even after the user selects a different one through UI interaction.

What is the correct way to programatically select a default choice Chip that is a child of a ChipGroup in Android?

like image 567
Lorenzo Petroli Avatar asked May 13 '20 02:05

Lorenzo Petroli


People also ask

How to use ChipGroup in Android?

A ChipGroup is used to hold multiple Chip s. By default, the chips are reflowed across multiple lines. Set the app:singleLine attribute to constrain the chips to a single horizontal line. If you do so, you'll usually want to wrap this ChipGroup in a HorizontalScrollView .

What is Chip button in Android Studio?

Chips are compact elements that represent an attribute, text, entity, or action. They allow users to enter information, select a choice, filter content, or trigger an action. The Chip widget is a thin view wrapper around the ChipDrawable , which contains all of the layout and draw logic.


1 Answers

Found my answer.

  • Use View.generateViewId() and assign the new Id to the newly create chip. Then, add
  • Add chip to its parent ChipGroup
  • Check chip view ChipGroup using viewBinding.typeChipGroup.check(id)

This is the final code:

private fun setupTypeSelection(types: List<Trail.Type>) {

    types.forEach { type ->
        val chip = layoutInflater.inflate(R.layout.chip_trail_type, viewBinding.typeContainer, false) as Chip

        chip.id = View.generateViewId()
        /* Set chip details as usual */

        viewBinding.typeContainer.addView(chip)

        if (currentType == null) viewBinding.typeChipGroup.check(chip.id)
    }
}
like image 85
Lorenzo Petroli Avatar answered Oct 20 '22 17:10

Lorenzo Petroli