Melvin Kent
Melvin Kent

Reputation: 340

Material Button background gradient

Is there a way i could set a gradient color to a MaterialButton from the google material library. app:backgroundTint only sets color but not gradient color

Upvotes: 10

Views: 3062

Answers (4)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364694

MaterialButton ignored android:background until release 1.2.0-alpha06.

With this release you can use something like:

<Button
    android:background="@drawable/bg_button_gradient"
    app:backgroundTint="@null"
    ... />

If you require this functionality with earlier versions of the library you can use the AppCompatButton. Something like:

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

Upvotes: 5

SinaMN75
SinaMN75

Reputation: 7659

not sure about this but according to developer.android.com we can't even add android:background, it seems that adding drawable to materialButton is not possible.

Upvotes: 3

Anisuzzaman Babla
Anisuzzaman Babla

Reputation: 7490

There is no way to do it by using default method. Better use gradient drawable.

Create a new xml file and put it in drawable and then add it to button as background

gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
   <gradient
      android:startColor="#000000"
      android:centerColor="#ffffff"
      android:endColor="#cfcfcf"
      android:angle="270" />
</shape>

layout.xml

 <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient"
    android:text="YourText" >

Upvotes: 1

logoped583st
logoped583st

Reputation: 27

Create drawable file, for examle

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="135"
    android:centerColor="#6200ee"
    android:endColor="#6200ee"
    android:startColor="#3700b3"
    android:type="linear" />
 </shape>

and set as background of button this file

Upvotes: -2

Related Questions