How to Set a Gradient Background in a Material Button from Material Components

How to set a gradient background to a Material Button?

To use a custom drawable background with the MaterialButton you can use the android:background attribute:

<MaterialButton
app:backgroundTint="@null"
android:background="@drawable/bkg_button_gradient"
... />

NOTE: It requires at least the version 1.2.0-alpha06

Currently it is very important to add app:backgroundTint="@null" to avoid that the custom background doesn't get tinted by default with the backgroundTint color.

With lower versions of the Material Components Library you have to use an AppCompatButton.

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

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.

Gradient color using MaterialButton and shapeAppearance

Gradient color using Material Button:

Gradient MaterialButton

MaterialButton :
To use a custom drawable background with the MaterialButton background tint attribute should be null ref code:
app:backgroundTint="@null"

 <com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:text="@string/button_enabled"

app:backgroundTint="@null"
android:background="@drawable/gradient_1"
/>

Gradient with shape: res/drawable/gradient_1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#DD8E54E9"
android:startColor="#CC225AE0" />
<corners android:topLeftRadius="18dp"
android:bottomRightRadius="18dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
/>
<stroke
android:width="6dp"
android:color="#007879E8" />
</shape>

Background Gradient not working on Button and MaterialButton

There are two ways to accomplish

Change you Button or MaterialButton to AppCompatButton
or Use these theme instead

Have a look at this answer

Material Component theme is still under development .Read this article

Can't create gradient material design button

It won't work. Somehow I also tried that and it in the material button the gradient is not working and it has to do with the internals of material-designing.

Android Studio : Gradient background not showing in Button

Replace in Button with androidx.appcompat.widget.AppCompatButton in activity_main.xml

It seems that using Button it is not possible to have the complete personalization of the widget.

Button widget handles its own background and you can only use tinting to change its color. This is an open issue and hasn't been fixed yet.

android: background is not working in the material design button component

You just have to fix your gradient drawable file.

remove the item tag from it and put android xml namespace declaration like this :

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<gradient android:startColor="@color/start_color"
android:endColor="@color/end_color"/>
<corners android:radius="10dp"/>

</shape>

And add this line to your button xml code :

app:backgroundTint="@null"

without this your gradient background won't show up

Note : MaterialButton manages its own background to control elevation, shape, color and states. Consider using backgroundTint, shapeAppearance and other attributes where available. A custom background will ignore these attributes and you should consider handling interaction states such as pressed, focused and disabled

You are also going to lose ripple effect when using custom background.

Why is my android button not showing the correct background?

As it is stated in the @Mariusz answer all attributes from Material Button is not supported.

On the other hand, with recent versions of material design you can set a custom background for your Material Button

Material Library :

implementation 'com.google.android.material:material:1.2.1'

You must set background tint attribute "null", put this in your style folder

<style name="my_button" parent="Widget.MaterialComponents.Button">
<item name="android:background">@drawable/register_button_bg</item>
<item name="textAllCaps">false</item>
<item name="backgroundTint">@null</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
</style>

Set your button style to "my_button"

    <com.google.android.material.button.MaterialButton
android:id="@+id/register_fragment_submit_button"
style="@style/my_button"
....
/>


Related Topics



Leave a reply



Submit