Search code examples
androidkotlinandroid-themematerial-components-androidandroid-chips

Material Chip input widget can only be used with a Theme.AppCompat


I'm trying to make a ChipInput in my Android App after i've created my layout with a AutoCompleteEditText and a ChipGroup to which i'm adding the dynamically created Chip i get the following error in stacktrace:

E/ThemeUtils: View class com.google.android.material.chip.Chip is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).

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

The issue is that my Theme is yet set to Material but i still get that error, i've tried even to set the theme programmatically to the Chip but still having that error.

Here is the code where i add the chip programmatically

private fun addChipToGroup(person: String, chipGroup: ChipGroup) {
    val chip = Chip(applicationContext)
    chip.text = person
    chip.isCloseIconVisible = true

    // necessary to get single selection working
    chip.isClickable = true
    chip.isCheckable = false
    chipGroup.addView(chip as View)
    chip.setOnCloseIconClickListener { chipGroup.removeView(chip as View) }
}

And the app crash at the first line of that method


Solution

  • Your issue is here:

    val chip = Chip(applicationContext)
    

    The Application context doesn't have your app theme.
    You have to use a themed context like an Activity.