Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 792
Thibaud Renaux Avatar asked Jun 17 '18 16:06

Thibaud Renaux


People also ask

What is MaterialCardView in Android?

MaterialCardView is a customizable component based on CardView from the Android Support Library. MaterialCardView provides all of the features of CardView , but adds attributes for customizing the stroke and uses an updated Material style by default.

What is Coloronprimary in Android?

colorPrimary and colorSecondary represent the colors of your brand. colorPrimaryVariant and colorSecondaryVariant are lighter or darker shades of your brand colors. colorSurface is used for “sheets” of material (like cards and bottom sheets) android:colorBackground is the window background color of your app.


1 Answers

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.

like image 82
Eugen Pechanec Avatar answered Nov 16 '22 03:11

Eugen Pechanec