Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material components not using color accent declared in theme

My main app theme parent is MaterialComponents theme. I changed the colors, but material components (MaterialButton, InputLayout, EditText) are not using my accent color (they are using some blue color, my declared Accent color is Teal) What is the issue? How is the best way to deal with theming Material Components?

My main 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>
</style>

Edit: Changing theme to Theme.MaterialComponents.Bridge is not solving that problem.

like image 749
OMIsie11 Avatar asked Sep 21 '18 12:09

OMIsie11


3 Answers

Maybe you should try to use colorSecondary instead of colorAccent:

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Original AppCompat attributes. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorSecondary">@color/colorAccent</item>
</style>

This works for me

like image 113
Casper Halleriet Avatar answered Oct 05 '22 20:10

Casper Halleriet


The issue in my case was a different styles.xml declared for the specific API. I made a mistake in it, and when testing, that other values-v21/styles.xml was applied before styles.xml.

So long story short, check if the mistake is not present in other variants. I declared that variant wrong.

like image 22
OMIsie11 Avatar answered Oct 05 '22 19:10

OMIsie11


For those, trying to use a theme that inherits from Theme.MaterialComponents.* please also keep in mind not to use a regular Button class but com.google.android.material.button.MaterialButton instead.

Instead of:

<Button
  (...)
  style="@style/Widget.MaterialComponents.Button" />

Make sure to use:

<com.google.android.material.button.MaterialButton
  (...)
  style="@style/Widget.MaterialComponents.Button" />

Been there. Nothing to be ashamed of. :)

like image 27
Tomasz Dzieniak Avatar answered Oct 05 '22 19:10

Tomasz Dzieniak