0

How can I add a touch feedback like a ripple to my Custom View?

I could detect the Touch Events but I want simple like a ripple to happen when my custom view is touched. is that possible?

Thanks in advance.

Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107

2 Answers2

0

You just have to play a xml animation for your view.

Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.my_amazing_ripple_effect);

myCustomView.startAnimation(anim);

You can find many xml animations all over the internet, but you can also create your own (which I highly recommend).

Let me know if this helped.


UPDATE:

Sample code:

View myView = ...some view; // grab your view here

myView.setOnClickListener( v -> { // this is a lambda expression. It is only used to shorten the code and improve its legibility. You could also use myView.setOnClickListener(new View.OnClickListener() { }

    Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.my_amazing_ripple_effect);

    myView.startAnimation(anim);

});
Luís Henriques
  • 604
  • 1
  • 10
  • 30
0

Use this compile 'com.balysv:material-ripple:1.0.2', Place your custom Widget inside MaterialRippleLayout, Here

  <com.balysv.materialripple.MaterialRippleLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                app:mrl_rippleAlpha="0.3"
                android:layout_weight="2"
                app:mrl_rippleColor="#80FF5722"
                app:mrl_rippleDimension="10dp"
                app:mrl_rippleHover="true"
                app:mrl_rippleOverlay="true">
            <com.your.custome.view
               />
            </com.balysv.materialripple.MaterialRippleLayout>

You can use this programmatically also

MaterialRippleLayout.on(view)
           .rippleColor(Color.BLACK)
           .create();
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105