Search code examples
androidgradientandroid-buttonmaterial-components-android

How to set a gradient background in a Material Button from Material Components?


I've been trying to set a gradient background in a Material Button from Material Components for Android, but it's not working. So, how to set a gradient background in a Material Button?


Solution

  • From the documentation for MaterialButton:

    Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton 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.

    The only supported way to modify the background of the button is to set a color value to the app:backgroundTint attribute. Unfortunately, gradients are not supported here either; you can only use a simple color value or a ColorStateList.

    So, there is no supported way to use a gradient background with MaterialButton.