lannyf
lannyf

Reputation: 11035

android, MaterialButton button color changed after adding style Theme.MaterialComponents.DayNight

having a MaterialButton, with style:

<style name="InstallButtonStyle" parent="Widget.MaterialComponents.Button">
        <item name="materialThemeOverlay">@style/InstallButtonThemeOverlay</item>
    </style>

    <style name="InstallButtonThemeOverlay">
        <item name="colorPrimary">@color/btn_color</item>
    </style>

and color is blue:

<color name="btn_color">#E6188FFF</color>

It has been working, but when the aar is used in a app which does not using the material them the app crashes,

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.google.android.material.button.MaterialButton

Have to adding the android:theme="@style/Theme.MaterialComponents.DayNight" to the button, the app does not crash anymore, but the color changed to purple

enter image description here

<com.google.android.material.button.MaterialButton
                    android:theme="@style/Theme.MaterialComponents.DayNight"
                    android:id="@id/btn"
                    style="@style/InstallButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_marginBottom="10dp"
                    app:cornerRadius="20dp"
                    android:layout_gravity="bottom|right"
                    android:text="Install Now"
                    android:textAllCaps="false"/>

How to keep the color as defined in the style?

Upvotes: 1

Views: 958

Answers (1)

lannyf
lannyf

Reputation: 11035

adding the "backgroundTint" made it work, not sure if there is better way:

<style name="AdInstallButtonStyle" parent="Widget.MaterialComponents.Button">
        <item name="materialThemeOverlay">@style/InstallButtonThemeOverlay</item>
        <item name="backgroundTint">@color/btn_color</item>
    </style>

Upvotes: 2

Related Questions