How to set button background drawable image with ripple effect in android
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>
When the button has a background from the drawable, we can add ripple effect to the foreground parameter.. Check below code its working for my button with a different background
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_login_button"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:text="@string/action_button_login"
/>
Add below parameter for ripple effect
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"