How can I add a gradient background to a button

Issue

I am trying to create a gradient background on a button, but when I follow the answers of previously asked question to do this, all I get is the default purple.

Below is my code as an example:

layout.xml

<Button
        android:id="@+id/instagram_button"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="@drawable/gradient_shape"
        android:text="Open Instagram Profile"
        ></Button>

gradient_shape.xml

android:shape="rectangle">
<gradient
    android:angle="270"
    android:centerColor="#F82634"
    android:endColor="#FA3B6F"
    android:startColor="#FA7D6F" />

<padding
    android:bottom="7dp"
    android:left="7dp"
    android:right="7dp"
    android:top="7dp" />

<corners android:radius="60dp" />

Button background I am getting

Solution

|Final Update|: Changed the button to an ImageButton and set the srcCompat to the drawable I wanted and this method worked! To get text with my button on top of each other, I used a Frame layout to encapsulate them all as below.

 <FrameLayout
    android:layout_width="380dp"
    android:layout_height="50dp"
    android:layout_gravity="center">
        <ImageButton
            android:id="@+id/image_button"
            android:text="Open Instagram Profile"
            android:layout_width="380dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:backgroundTint="@null"
            android:background="@null"
            app:srcCompat="@drawable/custom_button" />

    <Button
        android:id="@+id/instagram_button"
        android:layout_width="380dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="@null"
        android:backgroundTint="@null"
        android:text="Open Instagram Profile"
        android:alpha="0"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Open Instagram Profile"
        android:gravity="center"
        android:textSize="20dp"
        android:textColor="@color/white"
        ></TextView>
</FrameLayout>

Answered By – Nice Question

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published