androidbackgroundandroid-drawableripple

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


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.


Solution

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