How to fill chip group dynamicallly in kotlin?

963 views Asked by At

I have a class of items which contains a String array of keywords. I will try to show every question in a recycler view using Kotlin and every keyword in the new chip. This chips will be included inside the chip group. My chip group XML code is

<com.google.android.material.chip.ChipGroup
            android:id="@+id/chpKeyword"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:singleSelection="true"
            android:textAppearance="@style/TextAppearance.MaterialComponents.Chip">

        </com.google.android.material.chip.ChipGroup>
  

and my try in the onViewCreated function to fill chip group is

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

   val chip= Chip(chpKeyword.context)
    chip.text="hello"
    chip.isClickable = true
    chip.isCheckable = true
    chip.setTextAppearance(R.style.TextAppearance_MaterialComponents_Button)
    chpKeyword.addView(chip)
}

But the issue is that when the view is created the app is stopped and throws the below error.

java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

I checked How can I add the new android chips dynamically in Android? but it did not solve my issue.

I want to know how can I resolve this error?

Thank you

1

There are 1 answers

0
Sylwek845 On BEST ANSWER

This error means that your app need to have this theme set Theme.MaterialComponents

Please go to res -> values -> styles and set parent as parent="Theme.MaterialComponents"

Then try running your app again.