Thibaud Renaux
Thibaud Renaux

Reputation: 280

MaterialCardview requires Theme.AppCompat

I'm trying to test my skills on new Google Material components. But for now I am encountering a problem with MaterialCardView

The building process tells me

The style on this component requires your app theme to be Theme.AppCompat 
[..]
at com.google.android.material.card.MaterialCardView.<init>

With this clue I added style="@style/Theme.AppCompat" & android:theme="@style/Theme.AppCompat" to the MaterialCardView and also to my Activity in the manifest.

I tried also to change my Acitivity to AppCompatActivity but without any success.

I also tried to set styles told by material.io documentation but without success !

Have you some clues?

Thanks

Upvotes: 10

Views: 2038

Answers (1)

Eugen Pechanec
Eugen Pechanec

Reputation: 38243

According to Material Components 1.2.1 you need to do this:

  • Depend on the library implementation 'com.google.android.material:material:1.2.1'
  • You'll need to have compileSdkVersion 30
  • Your activity needs to extend AppCompatActivity (or use AppCompatDelegate)
  • You have to use a Material Components theme

Source: https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md

The easiest way to get started is for your current theme to extend Theme.MaterialComponents.*.Bridge instead of Theme.AppCompat.*.

Additionally you'll need to override the following attribute in your theme, otherwise the card color will be broken:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
    <item name="elevationOverlayEnabled">false</item>
</style>

Don't set android:theme="@style/Theme.MaterialComponents" on the card. You'll lose color information (primary, secondary, accent,...) from your theme on every widget inside the card.

Don't set style="@style/Theme.MaterialComponents on the card. Don't mix themes and styles.

Upvotes: 5

Related Questions