Why do I need to add the line "android:theme="@style/Theme.MaterialComponents" in order for my FloatingActionButton to work?

Viewed 406

I'm inflating a layout using layout Inflater

My theme is already MaterialComponents

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

But when I try to inflate the layout with this FAB,

R.layout.test_view

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorPrimary"
        android:layout_gravity="center"/>
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
view = (ViewGroup) layoutInflater.inflate(R.layout.test_view, null);  

It's throwing the errors

Binary XML file line #21: Binary XML file line #21: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton

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

The above error doesn't make sense to me either because the FAB is material, not appcompat.

Unless I add this line:

android:theme="@style/Theme.MaterialComponents"

<com.google.android.material.floatingactionbutton.FloatingActionButton

        android:theme="@style/Theme.MaterialComponents" 

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorPrimary"
        android:layout_gravity="center"/>

Why do I need this line when my entire app theme is already set as Material for the application in my manifest?

<application android:theme="@style/AppTheme"

Dependencies:

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.0-alpha05'
0 Answers
Related