Search code examples
androidandroid-custom-viewtouch-feedback

Added A Touch FeedBack to Custom View


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.


Solution

  • 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);
    
    });