Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

  • #DF2D48 @ 0.0
  • #D72D46 @ 0.2
  • #AF223B @ 0.5
  • #AC2139 @ 1.0

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 ?

like image 903
Jéwôm' Avatar asked Jan 24 '23 20:01

Jéwôm'


1 Answers

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>
like image 142
Gabriele Mariotti Avatar answered Feb 11 '23 16:02

Gabriele Mariotti