Hi everyone.
I'm trying to dynamically inflate a button layout into a recycler view item from my item adapter.
Here is how it looks :
LayoutInflater factory = LayoutInflater.from(mContext);
View buttonView = factory.inflate(buttonsList.get(position).getLayout(), null);
holder.buttonContainer.addView(buttonView);
Basically, my getLayout attribute returns the layout id of the corresponding button item.
Layout looks like this :
<com.google.android.material.button.MaterialButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/material_button"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:text="Search"
android:textSize="14sp"
android:textAllCaps="true"
android:fontFamily="@font/raleway_semibold" />
I can inflate basic buttons from the old AppCompat library, but not from the Material Design.
I get the following error :
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.google.android.material.button.MaterialButton
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.google.android.material.button.MaterialButton
Do you have any idea where the problem comes from ? Is it a library bug ?
Thanks in advance,
Corentin
Try to update your app theme to inherit from Theme.MaterialComponents (or a descendant)
If you are still having an issue that you think is a bug, please submit it at https://issuetracker.google.com/issues/new?component=439535
@houdayec I faced this issue before and it was because the android:textAppearance attribute was missing.
Just add the below attribute and your problem should be fixed.
android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
@m-Samhoury I had to add yours plus another line to MaterialButton attributes:
android:theme="@style/FormButton"
android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
Had to create my own style
<style name="FormButton" parent="Theme.MaterialComponents">
<item name="colorOnPrimary">@android:color/holo_red_light</item>
<item name="colorPrimary">@android:color/holo_orange_dark</item>
<item name="colorOnSurface">@android:color/holo_orange_light</item>
</style>
Then it worked.
Change your AppTheme parent to Theme.MaterialComponents
Before
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
After
<!-- Material application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Change your AppTheme parent to Theme.MaterialComponents
Before
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> </style>After
<!-- Material application theme. --> <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Customize your theme here. --> </style>
Thank you Twibap
This worked for me
change in gradle not work for me. but adding in xml
android:theme="@style/FormButton"
android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
work for me
check your style file whether is it define as AppCompa or MaterialComponents.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
if it is AppCompat yo are trying material but your global one is set to Material. Thatswhy error is showing
Issue still happening even after setting all the properties commented above. Using com.google.android.material:material:1.1.0
Try to change theme attribute into application node inside manifest file!!!
Changed the style and it works perfectly!
Old =
Most helpful comment
Try to update your app theme to inherit from Theme.MaterialComponents (or a descendant)