How to set button background drawable image with ripple effect in android

Prithiv Dharmaraj picture Prithiv Dharmaraj · Sep 28, 2016 · Viewed 11.2k times · Source

My Xml code:

<Button
    android:id="@+id/link_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/google_btn" />

I am applying Default ripple effect

<Button
    android:id="@+id/link_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground" />

but I need button background "@drawable/google_btn" with
"?android:attr/selectableItemBackground". it's means i need ripple effect with custom background.

Answer

Zahidul Islam picture Zahidul Islam · Sep 28, 2016

In your drawable-v21 folder you can write code for ripple effect by your own. Make a drawable xml file and set starting tag by ripple. Like this :

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccentDark">
    <item>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item
                android:drawable="@color/button_accent_dark"
                android:state_checked="false"/>
            <item
                android:drawable="@color/button_accent"
                android:state_checked="true" />
            <item
                android:drawable="@color/button_accent_dark" />

        </selector>
    </item>
</ripple>