Error : IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents
Solution 1
There is some issue with material:1.1.0-alpha01
A simple solution is to change the parent theme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Solution 2
Extend your base app Theme from Material Components Bridge theme. It extends AppCompat theme, but adds all necessary stuff.
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->
</style>
Both
Theme.MaterialComponents
andTheme.MaterialComponents.Light
have.Bridge
themes:
Theme.MaterialComponents.Bridge
Theme.MaterialComponents.Light.Bridge
Theme.MaterialComponents.NoActionBar.Bridge
Theme.MaterialComponents.Light.NoActionBar.Bridge
Theme.MaterialComponents.Light.DarkActionBar.Bridge
It allows you to keep using latest version of the library, but avoid a lot of UI issues, caused by migration to pure Material Component theme
https://material.io/develop/android/docs/getting-started/#bridge-themes
Solution 3
Adding theme directly to widget works too. E.g.:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_button_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/Theme.MaterialComponents.Light"> <======== THIS
Checked with min. SDK ver. 21.
Solution 4
None of these worked for me. Must do these two things.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme -->
</style>
and
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
app:theme="@style/Theme.MaterialComponents"
Solution 5
|> 1: Define the following style in style.xml
:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme -->
</style>
|> 2: In the manifest.xml
file, use from above style:
<application
android:hardwareAccelerated="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"> <!-- IMPORTANT (This was my mistake) -->
</application>
|> 3: Choose a style for your component:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
style="@style/Widget.MaterialComponents.BottomAppBar"
...
>
</com.google.android.material.bottomappbar.BottomAppBar>
Goku
serial up-voter/down-voter God Bless You keep patience karma will work
Updated on February 17, 2022Comments
-
Goku over 2 years
Below are my dependencies
implementation 'com.google.android.material:material:1.0.0' implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
My layout.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:fitsSystemWindows="true"> <com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomBar" style="@style/Widget.MaterialComponents.BottomAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:backgroundTint="@color/colorPrimary" app:fabAlignmentMode="center" app:fabCradleMargin="10dp" app:fabCradleVerticalOffset="4dp" app:navigationIcon="@drawable/ic_drawer" > </com.google.android.material.bottomappbar.BottomAppBar> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_anchor="@id/bottomBar" app:srcCompat="@drawable/ic_apps" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
The Above code is working fine for me
But when i update the
dependencies
ofmaterial design
implementation 'com.google.android.material:material:1.1.0-alpha01'
I'm getting below error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{neel.com.bottomappbar/neel.com.bottomappbar.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.bottomappbar.BottomAppBar at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.bottomappbar.BottomAppBar at android.view.LayoutInflater.inflate(LayoutInflater.java:543) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at neel.com.bottomappbar.MainActivity.onCreate(MainActivity.kt:16) at android.app.Activity.performCreate(Activity.java:6358) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.bottomappbar.BottomAppBar at android.view.LayoutInflater.createView(LayoutInflater.java:649) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at neel.com.bottomappbar.MainActivity.onCreate(MainActivity.kt:16) at android.app.Activity.performCreate(Activity.java:6358) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:623) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at neel.com.bottomappbar.MainActivity.onCreate(MainActivity.kt:16) at android.app.Activity.performCreate(Activity.java:6358) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant). at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240) at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215) at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143) at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:78) at com.google.android.material.bottomappbar.BottomAppBar
-
Manuel over 4 yearsIssue still exists in
com.google.android.material:material:1.1.0
. -
Alirezaarabi about 4 yearsI got the same issue in
com.google.android.material:material:1.1.0
but solved that by changing the parent theme. -
Allen almost 4 yearsThis is working but might change your design a bit.
-
SimpleGuy almost 4 yearsSolution #1 works. Issue still persists wit Solution #2.
-
N Droidev almost 4 yearsThis changes view of components, so design.. And this is what said by Android Studio anyway.. This is not good solution
-
Liker777 about 3 yearsThank you so much. I believe this one should be the best answer because you are the only one who mentioned that the change should be made in style.xml file. (which is in the values folder)
-
Fazal Hussain almost 3 yearsHow can I do it for programmatically created chip?
-
Admin over 2 yearsPlease add further details to expand on your answer, such as working code or documentation citations.
-
Danil.B over 2 yearsThats way better than changing entire app theme
-
Carlos López Marí over 2 yearsthank you very much
-
Zhou Hongbo over 2 yearsIt's recommended to use
.Bridge
Theme, such asTheme.MaterialComponents.Light.DarkActionBar.Bridge
, as you can use Material Design components without changing your app theme. See material.io/develop/android/docs/getting-started#bridge-themes -
aeracode about 2 years@FazalHussain
new Chip(new ContextThemeWrapper(context, R.style.Theme_MaterialComponents_Light));
-
MosesK almost 2 yearsThis worked for me. For some Reason my CustomAdapter was unable to get the app theme and I had to add it in that xml.