7

I have been trying to achieve button like in attached image & for that I have created gradient SVG with shadow so that it looks like elevation but when this svg applied to button as background drawable it looks flat & ripple effect is also gone.

I want to achieve same button as below image : enter image description here

And here is my code :

<Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="35dp"
            android:enabled="false"
            android:elevation="10dp"
            android:translationZ="10dp"
            android:stateListAnimator="@null"
            android:background="@drawable/bg_gradient"
            android:text="@string/btn_txt_login"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

I'm using SVG file for creating background gradient with rounded border. Any help would be appreciated.

Deep Shah
  • 1,334
  • 3
  • 20
  • 41
  • You can try ImageButton [Image button with ripple](https://stackoverflow.com/questions/30959610/apply-material-design-touch-ripple-to-imagebutton) or use gradient as android:foreground and ?android:attr/selectableItemBackground as background – Shripad Jadhav Sep 08 '18 at 09:13

3 Answers3

3

I have a solution for you.

Add this into your build.gradle(Module:app) dependency:

implementation 'com.android.support:cardview-v7:27.1.1'

Now use CardView layout instead of Button:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="4dp">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center_vertical|center_horizontal"
    android:textSize="18sp"
    android:text="LOGIN"
    android:textColor="@android:color/white"
    android:textStyle="bold"
    android:background="@drawable/gradient"/>

</android.support.v7.widget.CardView>

Create gradient.xml file into drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:startColor="#8b0ab7"
        android:endColor="#0b85d0"
        android:angle="0"/>

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

</shape>
Pang
  • 9,564
  • 146
  • 81
  • 122
Ravi
  • 319
  • 4
  • 13
1

Add these attributes, if still it doesn't work use cardview

android:clipChildren="false"
        android:clipToPadding="false"

and here is your code

    <Button     android:id="@+id/btn_login"
                android:layout_width="match_parent"
                android:layout_height="52dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="35dp"
                android:enabled="false"
                android:elevation="10dp"
                android:translationZ="10dp"
                android:stateListAnimator="@null"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:background="@drawable/bg_gradient"
                android:text="@string/btn_txt_login"
                android:textColor="@android:color/white"
                android:textSize="18sp" />
varun setia
  • 148
  • 10
0

Please try with using the following drawable content.

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorDefaultButtonRipple">
    <item>
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/darker_gray" />
                    <corners android:radius="15dp" />
                </shape>
            </item>
            <item android:bottom="2dp" android:left="2dp">
                <shape>
                    <gradient android:angle="0" android:endColor="#5789bc" android:startColor="#8e6fa6" />
                    <corners android:radius="15dp" />
                    <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>
</ripple>
Anupa Dayaratne
  • 358
  • 1
  • 8