Jéwôm'
Jéwôm'

Reputation: 3971

Gradient color using MaterialButton and shapeAppearance

I want to make a Button like this one : enter image description here

I try it using style with shapeAppearance :

<style name="ShapeAppearance.Button" parent="ShapeAppearance.MaterialComponents">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopLeft">@dimen/button_corner_radius</item>
    <item name="cornerSizeTopRight">5dp</item>
    <item name="cornerSizeBottomRight">@dimen/button_corner_radius</item>
    <item name="cornerSizeBottomLeft">5dp</item>
</style>

<dimen name="button_corner_radius">40dp</dimen>

I apply the style in my MaterialButton like this :

app:shapeAppearance="@style/ShapeAppearance.Button" 

The result is :

enter image description here

Now, I try to put a linear gradient :

For example :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#DF2D48"
        android:centerColor="#D72D46"
        android:endColor="#AC2139"
        android:angle="360" />
</shape>

and I apply it using in my Shape style :

<item name="android:background">@drawable/test</item>

But the result is the same. Why ?

Upvotes: 4

Views: 3104

Answers (3)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364694

About the MaterialButton:

  • it uses a MaterialShapeDrawable to apply the shapeAppearance (shape and stroke).
    Currently (1.3.0) the MaterialShapeDrawable doesn't support a gradient background.

  • it supports the android:background attribute only starting from the 1.2.0-alpha06. But using the android:background the MaterialButton removes the default MaterialShapeDrawable background and the shapeAppearance is not applied.

  • if you are using a MaterialComponents Theme there is no difference between <Button /> and <com.google.android.material.button.MaterialButton /> because of the MaterialComponentsViewInflater that replaces the Button with a MaterialButton at runtime.

You have 2 solutions:

  • to use a custom shape with custom rounded corners and a gradient color and apply it as android:background to MaterialButton

  • to use a custom shape and apply it to an AppCompatButton.

With MaterialButton :

    <com.google.android.material.button.MaterialButton
        app:backgroundTint="@null"
        android:background="@drawable/gradient_shape"
        />

enter image description here

Make sure to null out backgroundTint (either app:backgroundTint="@null" or app:backgroundTint="@empty"), to avoid that the custom background doesn't get tinted.
Note: it requires at least the version 1.2.0-alpha06.

With AppCompatButton :

  <androidx.appcompat.widget.AppCompatButton
      android:background="@drawable/gradient_shape"/>

enter image description here

with a shape like:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="0"
        android:endColor="...."
        android:startColor="...." />

    <corners android:topLeftRadius="40dp"
        android:bottomRightRadius="40dp"
        android:topRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        />

</shape>

Upvotes: 5

Bolt UIX
Bolt UIX

Reputation: 7032

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>

Upvotes: 5

J&#233;w&#244;m&#39;
J&#233;w&#244;m&#39;

Reputation: 3971

The problem is because I try to set a background to a MaterialButton, and it's not possible.

Finally, I've used a classic Button with this background :

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

    <gradient
        android:startColor="#D72D46"
        android:centerColor="#AF223B"
        android:endColor="#AC2139"
        android:angle="360" />

    <corners
        android:bottomRightRadius="40dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="40dp"
        android:topRightRadius="5dp" />

</shape>

Upvotes: 1

Related Questions