Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create android button background svg with elevation?

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.

like image 857
Deep Shah Avatar asked Sep 08 '18 09:09

Deep Shah


1 Answers

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>
like image 190
Ravi Avatar answered Oct 06 '22 06:10

Ravi