Search code examples
javaandroidmaterial-componentsmaterial-components-androidandroid-chips

This component requires that you specify a valid TextAppearance attribute


My app theme extends from AppCompat but I want to use Chip dynamically but when I added Chips dynamically app crashing with exception

This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).

but when I extend app theme to Material app doesn't crash and filter chips works fine but my problem is I dont want to extend theme from Material because it effects my app UI, how can I use chip in this scenario?

Edit: used bridge, it saves from crashing but when I click chip it does not change appearance of chip that I was clicked

Chip chip = new Chip(getContext());
        ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(getContext(), null, 0, R.style.Widget_MaterialComponents_Chip_Filter);
        chip.setChipDrawable(chipDrawable);
        chip.setCheckable(true);
        chip.setText(itemArrayList.get(i).getName());
        binding.rvReport.addView(chip);

Thanks


Solution

  • You can check the official documentation:

    Your app theme should inherit from a Material Components theme.
    If you can't change your theme, you can do one of the following:

    • Inherit from one of our Material Components Bridge themes
    • Continue to inherit from an AppCompat theme and add some new theme attributes to your theme.

    For the Chip you can also define the layout as follows:

    <com.google.android.material.chip.Chip
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/Widget.MaterialComponents.Chip.Filter"
        .../>
    

    and then use this code:

    Chip chip =
          (Chip) getLayoutInflater().inflate(R.layout.single_chip_layout, chipGroup, false);
    //...
    chipGroup.addView(chip);