I want to make a Button
like this one :
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 :
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 ?
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"
/>
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"/>
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>
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