I'm using the new material components com.google.android.material:material
with android x but I can't set a custom background to the button.
I know that I can use app:backgroundTint
to change the color
but the default background has some padding that I want to get rid of, and the old way of using android:background
to set my own background but this is no longer working.
I looked at the docs but can't find any mention to this change.
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.
I try change color to MaterialButton with this code: var materialButton = findViewByid(R.id....) as MaterialButton materialButton. setBackgroundColor( ContextCompat. getColor(this@MyActivity, R.
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" ... />
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With