lannyf
lannyf

Reputation: 11035

android, how to use MaterialCardView while using Theme.AppCompat.Light.DarkActionBar

The app is using androidX with minSdk 21, and using

AppCompatActivity with theme:

<style name="myAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/theme_colorPrimary</item>
        <item name="colorPrimaryDark">@color/theme_colorPrimaryDark</item>
        <item name="colorAccent">@color/theme_colorAccent</item>
    </style>

for the AppCompat DayNight theme, and switching day or night theme like:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) // or AppCompatDelegate.MODE_NIGHT_NO

But when uses com.google.android.material.card.MaterialCardView (with implementation 'com.google.android.material:material:1.1.0')

<com.google.android.material.card.MaterialCardView
        style="@style/myCardViewStyle"
        ...>

    </com.google.android.material.card.MaterialCardView>

...

  <style name="CustomCardViewStyle" parent="@style/Widget.MaterialComponents.CardView">
     <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay_card_custom_corners</item>
  </style>

<style name="ShapeAppearanceOverlay_card_custom_corners" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">8dp</item>
    <item name="cornerSizeBottomLeft">8dp</item>
  </style>

it got crash:

android.view.InflateException: Binary XML file line #573: Binary XML file line #573: 
Error inflating class com.google.android.material.card.MaterialCardView

What could be the cause? If using MaterialCardView, what theme should use and still be able to do

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

Note:

In the Getting started with Material Components for Android, it says Change your app theme to inherit from a Material Components theme, and

Material Components themes
The following is the list of Material Components themes you can use to get the latest component styles and theme-level attributes.

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight
Theme.MaterialComponents.DayNight.NoActionBar
Theme.MaterialComponents.DayNight.DarkActionBar
Update your app theme to inherit from one of these themes, e.g.:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

I saw the Theme.AppCompat.Light.DarkActionBar used in the app is in derive chain eventually back up to android:Theme.Material.Light.NoActionBar

<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>
<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light">
<style name="Base.Theme.AppCompat.Light" parent="Base.V21.Theme.AppCompat.Light"/>
<style name="Base.V21.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light">
<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
<style name="Platform.AppCompat.Light" parent="Platform.V21.AppCompat.Light"/>

<style name="Platform.V21.AppCompat.Light" parent="android:Theme.Material.Light.NoActionBar">

and the Theme.MaterialComponents.Light.DarkActionBar is deprived from android:Theme.Material.Light.NoActionBar

Upvotes: 2

Views: 1140

Answers (1)

Wilson Sim
Wilson Sim

Reputation: 521

Try using this in your MaterialCardView?

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

Adding this in your MaterialCardView will not affect your app theme I believe.

Upvotes: 3

Related Questions