Android material chip component crashing app. Unable to inflate xml
S

5

58

Tried Chip component from both support library
com.google.android.support:design:28.0.0-rc01 and material
com.google.android.material:material:1.0.0-rc01

StackTrace

android.view.InflateException: Binary XML file line #72: Binary XML file 
      line #72: Error inflating class com.google.android.material.chip.Chip 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:429)`

Layout

<com.google.android.material.chip.Chip
    android:id="@+id/chip"
    style="style/Widget.MaterialComponents.Chip.Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/account"
    app:closeIconEnabled="true" />`
Streptococcus answered 16/8, 2018 at 13:4 Comment(6)
Did you tried with implementation 'com.android.support:design:28.0.0-rc01'Brant
Yes i did. Same issueStreptococcus
Check my answerBrant
Are you extending on of the provided themes as explained in material.io/develop/android/docs/getting-started?Representative
@Representative thanks for the info.. Actually I didn't do that. Will get back after checking.Streptococcus
@Representative Thanks man it worked perfectly.Streptococcus
E
117

Update your app theme to inherit from one of these themes:

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar

For example:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

Note: Using a Material Components theme enables a custom view inflater

Source: https://www.material.io/develop/android/docs/getting-started/

Easing answered 13/9, 2018 at 13:34 Comment(4)
I was wondering why any of com.google.android.material widget is not inflating in the app. This is the reason!Strapped
In my case, val chip = new Chip(context) and val chip = layoutInflater.from(context).inflate were both failing despite my material theme. This is because I passed the wrong context (application context instead of activity context). Therefore, my material theme wasn't yet applied.Vivianne
*.Bridge is better for most cases. "If you use a bridge theme, you can start using Material Design components without changing your app theme" material.io/develop/android/docs/getting-started#bridge-themesSino
@ZhouHongbo, I changed Theme.MaterialComponents.Light.NoActionBar to Theme.MaterialComponents.Light.NoActionBar.Bridge and an application crashed. :-)Krigsman
D
82

You can juste add @style/Theme.MaterialComponents.Light style attribut in xml layout like this:

<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chip"
android:theme="@style/Theme.MaterialComponents.Light"/>
Dore answered 16/7, 2019 at 15:2 Comment(2)
Thanks! It works like a charm without changing the App Theme attribute.Primaveria
getting The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).Academicism
G
5

Add the following new theme attributes to your existing app theme:

<style name="Theme.MyApp" parent="Theme.AppCompat">

    <!-- Original AppCompat attributes. -->
    <item name="colorPrimary">@color/my_app_primary_color</item>
    <item name="colorPrimaryDark">@color/my_app_primary_dark_color</item>
    <item name="colorAccent">@color/my_app_accent_color</item>

    <!-- New MaterialComponents attributes. -->
    <item name="colorSecondary">?attr/colorPrimary</item>
    <item name="scrimBackground">@color/mtrl_scrim_color</item>
    <item name="textAppearanceHeadline1">@style/TextAppearance.MaterialComponents.Headline1</item>
    <item name="textAppearanceHeadline2">@style/TextAppearance.MaterialComponents.Headline2</item>
    <item name="textAppearanceHeadline3">@style/TextAppearance.MaterialComponents.Headline3</item>
    <item name="textAppearanceHeadline4">@style/TextAppearance.MaterialComponents.Headline4</item>
    <item name="textAppearanceHeadline5">@style/TextAppearance.MaterialComponents.Headline5</item>
    <item name="textAppearanceHeadline6">@style/TextAppearance.MaterialComponents.Headline6</item>
    <item name="textAppearanceSubtitle1">@style/TextAppearance.MaterialComponents.Subtitle1</item>
    <item name="textAppearanceSubtitle2">@style/TextAppearance.MaterialComponents.Subtitle2</item>
    <item name="textAppearanceBody1">@style/TextAppearance.MaterialComponents.Body1</item>
    <item name="textAppearanceBody2">@style/TextAppearance.MaterialComponents.Body2</item>
    <item name="textAppearanceCaption">@style/TextAppearance.MaterialComponents.Caption</item>
    <item name="textAppearanceButton">@style/TextAppearance.MaterialComponents.Button</item>
    <item name="textAppearanceOverline">@style/TextAppearance.MaterialComponents.Overline</item>

</style>

Source: https://material.io/develop/android/docs/getting-started/
Thanks @Paranoid42

Gargle answered 24/8, 2018 at 15:47 Comment(2)
Yes initially that was the problem.. I was using appcompat themeStreptococcus
Adding these shows a black screen when using the chip but doesnt crash atleastGambill
S
4

Finally Got a solution as how to generate chips dynamically using new material chips

Google has not provided any documentation for chipsInput layout or how to make a gmail address like layout. Using ChipDrawable won't give delete operation on chip option as can't call setOnCloseIconClickListener. I was able to do using HorizontalScrollView, ChipGroup and EditText

Here's my code sample :

MaterialChipsInputDemo

Streptococcus answered 24/8, 2018 at 15:21 Comment(0)
S
1

You can use Theme.MaterialComponents.**.Bridge to replace your previous theme.

For example, if the old theme is Theme.AppCompat.DayNight.NoActionBar, the new theme will be Theme.MaterialComponents.DayNight.NoActionBar.Bridge.

See https://material.io/develop/android/docs/getting-started#bridge-themes

Sino answered 16/2, 2022 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.