Theme.MaterialComponents colorPrimary not working

Viewed 3154

I'm using Theme.MaterialComponent, but it not affects to my actionBar, why?

style.xml

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@color/windowBackground</item>
        <item name="android:textColor">@color/white</item>
    </style>

colors.xml

<resources>
    <color name="colorPrimary">#801336</color>
    <color name="colorPrimaryDark">#510a32</color>
    <color name="colorAccent">#ee4540</color>
    <color name="windowBackground">#2d142c</color>

    <color name="white">#FFF</color>
</resources>

enter image description here

1 Answers

Using just Theme.MaterialComponents as parent for your app theme will have no effect on the ActionBar like you noticed. You need to use one of the themes which inherit from the base theme.

  • Theme.MaterialComponents.NoActionBar if you want to use your own Toolbar as ActionBar and have a dark theme (white text on dark background colors)
  • Theme.MaterialComponents.Light for a light theme (black text on light background) with an ActionBar generated by the runtime
  • Theme.MaterialComponents.Light.NoActionBar for a light theme with your own Toolbar, no auto-generated ActionBar
  • Theme.MaterialComponents.Light.DarkActionBar for an overall light theme with the exception of a dark ActionBar
  • Theme.MaterialComponents.DayNight for a DayNight theme
  • Theme.MaterialComponents.DayNight.NoActionBar for a DayNight theme without auto-generated ActionBar
  • Theme.MaterialComponents.DayNight.DarkActionBar for a DayNight theme with a dark ActionBar

Note: you may use any color, no matter what theme you choose. But if you choose for example Theme.MaterialComponents.Light.DarkActionBar and set

 <item name="colorPrimary">@color/white</item>

then your ActionBar will be white and - since you said you have a dark ActionBar - the text will also be white. So it's up to you to make sure that there is enough contrast.

Related