0

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

2 Answers2

0

|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>
0

create new Drawable Resource write below code:

gradient_background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape>
        <corners android:radius="60dp"/>
        <gradient android:startColor="#fa7d6f"
            android:centerColor="#f82634"
            android:endColor="#fa3b6f"
            android:angle="180"/>
    </shape>
  </item>
</selector>

layout:

 <Button
     android:layout_width="250dp"
     android:layout_height="60dp"
     android:text="test button"
     android:textColor="@color/white"
     android:textStyle="bold"
     android:textSize="25sp"
     android:background="@drawable/gradient_background"/>

enter image description here