2

I am trying to play a ripple effect (from Android L) on a view at a certain time (not on the view being touched). To be specific, when the user successfully changes some text, I want a certain view to play a green ripple effect to show success. Is there any way to do this?

I have tried putting a RippleDrawable in an Animation, putting the RippleDrawable as the background for my "success view." But, I can't figure out how to play the ripple animation as I have described.

P.S. My project is Android L only right now, so I am not worried about backwards compatibility.

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • just guessing: (from the docs) `The default touch feedback animations for buttons use the new RippleDrawable class, which transitions between different states with a ripple effect.` so probably you want to call `setState` or something similar on the Drawable to trigger the effect. – Budius Aug 20 '14 at 15:34
  • @Budius Thank you. Sadly, int[] state = new int[] {android.R.attr.state_focused, android.R.attr.state_pressed}; view.getBackground().setState(state); didn't work. That was a great thought, though. Am I doing that right, btw? – Eric Cochran Aug 20 '14 at 16:30
  • How is your ripple defined? – alanv Aug 21 '14 at 00:55
  • @alanv http://pastebin.com/cKU5RpLP – Eric Cochran Aug 21 '14 at 01:02

1 Answers1

2

Since your mask is just a solid rectangle, you can simplify your ripple XML to:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:id="@android:id/mask" android:drawable="@android:color/white" />
    <item android:drawable="@drawable/file_item_selector"/>
</ripple>

You can manually trigger a ripple effect by forcing a view in and out of the pressed state.

myView.setPressed(true);

// For a quick ripple, you can immediately set false.
myView.setPressed(false);

// Or for a long ripple, you can post to a handler.
myView.postDelayed(new Runnable() {
    public void run() {
        myView.setPressed(false);
    }
}, 500 /* delay in milliseconds */);
alanv
  • 23,966
  • 4
  • 93
  • 80