2

I need ripple effect for buttons. Currently I'm using ripple background for buttons in lollipop and upward devices using an xml in v21 directory and it is working fine, but I also need a ripple effect for pre-lollipop devices.

Here are my buttons. [Button 1][1]

I used some custom libraries for acquiring ripple effect for pre lollipop devices. For example I used com.balysv:material-ripple:1.0.2 library by following how to create ripple effect for pre-lollipop. In there they use this library which uses a custom view to wrap the button.

So when I use that for my curved, shaped buttons in pre lollipop, the result would be like this...

enter image description here

As you can see, the ripple effect goes out side of the button stroke boundaries. Is there is a way to resolve this?

Also, is there a way to acquire ripple effect using android's default support libraries instead of using custom 3rd party libraries for pre lollipop devices?

Community
  • 1
  • 1
thilina Kj
  • 1,300
  • 13
  • 25
  • 1
    no support library provides ripple drawable on pre lollipop devices - you have to use 3rd party ones – pskink Feb 15 '17 at 09:28
  • [try this](http://michaelevans.org/blog/2015/05/07/android-ripples-with-rounded-corners/) – shadygoneinsane Feb 15 '17 at 09:35
  • thanks @shadygoneinsane. Ripple effect is working fine with borders in lollipop and upwards. But we cannot use ripple for pre lollipop devices as ripple effect came with material design in Lollipop. So my point is to acquire ripple effect with curved shape buttons for pre lollipop devices. – thilina Kj Feb 15 '17 at 10:09
  • [how to create ripple effect for pre-lollipop][3] link seems broken .. – shadygoneinsane Feb 15 '17 at 10:21
  • Can you edit your question to paste that link for library that you are currently using !! – shadygoneinsane Feb 15 '17 at 10:43

2 Answers2

0

No. You don't have the option for ripple on pre-lollipop using support library. You may use this library - easy to use. Perform a check on OS and then implement the ripple if you have to like this.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // set layout background as ripple_drawable
} else {
    // Implement the 3rd party library
}
Debdeep
  • 752
  • 10
  • 22
  • Thanks @Debdeep. Even this one doesn't resolve my problem. Ripple effect is working in a rectangle. Not inside our custom shape. – thilina Kj Feb 15 '17 at 11:02
  • What you can also do is get a selectable background in which you can change colors. For that, you may keep your view inside a `CardView` and use `android:foreground="?android:attr/selectableItemBackground" android:clickable="true"` on the `CardView` – Debdeep Feb 15 '17 at 12:00
  • For the other views go through the last few lines in the comment [here](http://stackoverflow.com/a/33085033/5685358) . This might help. – Debdeep Feb 16 '17 at 06:30
  • Thanks @Debdeep. As I said earlier isn't that is for the lollipop and up devices?. I want the ripple effect for pre lollipop devices like kitkat. – thilina Kj Feb 16 '17 at 10:20
0

use https://github.com/ozodrukh/RippleDrawable.

ripple.xml

<ripple xmlns:android="htp://schemas.android.com/apk/res/android"
android:color="yourRippleColor">
<!-- style for ripple -->
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="yourButtonCornerRadius"/>
<solid android:color="#FF00FF"/>
</shape>
</item>
<!-- background -->
<item android:drawable="yourBackgroundForButton" />
</ripple>

Activity.java

Button bu=...;
bu.setBackground(LollipopDrawablesCompat.getDrawable(getResources(),R.drawable.ripple,getTheme());
bu.setOnTouchListener(new DrawableHotspotTouch((LollipopDrawable)bu.getBackground()));

tested on Samsung Galaxy Advance Duos, android 4.4.2(api 19,KitKat)