Can't use android:background with button from the new material components
The documentation for the MaterialButton
class says:
Do not use the
android:background
attribute.MaterialButton
manages its own background drawable, and setting a new background meansMaterialButton
can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed,MaterialButton
cannot guarantee well-defined behavior.
However, the GitHub readme says:
Note:
MaterialButton
is visually different fromButton
andAppCompatButton
. One of the main differences is thatAppCompatButton
has a4dp
inset on the left and right sides, whereasMaterialButton
does not.
This mentions only left/right inset, but the Attributes section of the readme shows that all four insets are supported:
So you could add these attributes to your <MaterialButton>
tag:
android:insetTop="0dp"
android:insetBottom="0dp"
In the Material Components Library, the MaterialButton
has a default style with insetBottom
and insetTop
with a value of 6dp
.
You can change it using:
<com.google.android.material.button.MaterialButton
android:insetTop="0dp"
android:insetBottom="0dp"
../>
If you want to change the background color you can use the app:backgroundTint
attribute or you can override some theme attributes from a default style then you can use new materialThemeOverlay
attribute.
In your case you can do something like:
<style name="MtButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
</style>
<style name="GreenButtonThemeOverlay">
<item name="colorPrimary">@color/green</item>
</style>
Finally starting with the version 1.2.0-alpha06
you can use the android:background
attribute in the MaterialButton
.
<MaterialButton
app:backgroundTint="@null"
android:background="@drawable/button_drawable"
... />
Looking at https://medium.com/@velmm/material-button-in-android-e4391a243b17 I found that app:backgroundTint
(and app:backgroundTintMode
) works. It changes a color, but not a drawable selector.
Also you can replace <Button
with <android.widget.Button
.