20

I have been developing an app for Lollipop (API 21).

When I change the Button color to something, the ripple effect doesn't works.

I found some third party libraries for the ripple effect, but I want to do this with standard API.

This answer didn't help either.

XML:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:background="@android:color/holo_blue_bright"
    android:layout_alignParentStart="true"
    android:layout_marginTop="76dp"
   />
Community
  • 1
  • 1
Zain Zafar
  • 1,607
  • 4
  • 14
  • 23

4 Answers4

31

You have to set your button's background to a RippleDrawable which you can define in XML. (I'll name it holo_blue_ripple.xml)

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/white"> <!-- ripple color -->

    <item android:drawable="@android:color/holo_blue_bright"/> <!-- normal color -->

</ripple>

Then reference it with android:background="@drawable/holo_blue_ripple".

Floern
  • 33,559
  • 24
  • 104
  • 119
27

Try this:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    ...
 />

Based on this tutorial and the official docs

kip2
  • 6,473
  • 4
  • 55
  • 72
7

Also ?attr/selectableItemBackgroundBorderless canbe used for circular ripple effect.

Androiderson
  • 16,865
  • 6
  • 62
  • 72
Meanman
  • 1,474
  • 20
  • 17
  • Doesn't work for me :/ Effect is- color change when clicked. My android version is 5.0 only. Could you help? – ugola Jul 06 '17 at 04:15
1

Try this attribute

android:background="?attr/selectableItemBackground"

This is the simplest way to show ripple effect in your view/layout.

swetabh suman
  • 1,949
  • 2
  • 19
  • 24
  • Doesn't work for me :/ Effect is- color change when clicked. My android version is 5.0 only. Could you help? adding this to a button – ugola Jul 06 '17 at 04:16