rive-android icon indicating copy to clipboard operation
rive-android copied to clipboard

RiveAnimationView's setOnClickListener doesn't work

Open frank-gyw opened this issue 3 years ago • 2 comments

Hi, I created a button animation file using Rive, however, when we use the button in Android, the setOnClickListener doesn't work. Can you let me know how to add setOnClickListener to the animation view? Thank you!

Here is the sample code.

val animationView = findViewById<RiveAnimationView>(R.id.android_player_view)
...
animationView.setOnClickListener { 
// Animation button is clicked, but it never gets called!!!!!!!
}

frank-gyw avatar Dec 01 '22 10:12 frank-gyw

I've fixed the onClickListener by doing this. Hope it helps anybody:

class RiveAnimationFixedView(context: Context, attrs: AttributeSet?) : RiveAnimationView(context, attrs) {
    private var startX: Float = 0f
    private var startY: Float = 0f
    private val viewConfiguration: ViewConfiguration = ViewConfiguration.get(context)
    private val scaledTouchSlop: Int = viewConfiguration.scaledTouchSlop

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN) {
            startX = event.x
            startY = event.y
        } else if (event?.action == MotionEvent.ACTION_UP) {
            val endX = event.x
            val endY = event.y
            if (abs(endX - startX) < scaledTouchSlop && abs(endY - startY) < scaledTouchSlop) {
                performClick()
            }
        }
        return super.onTouchEvent(event)
    }
}

kancic avatar Aug 02 '23 15:08 kancic

@HayesGordon @ErikUggeldahl Is this something that's on your team's radar? I noticed https://github.com/rive-app/rive-android/releases/tag/10.1.6 was recently released and included a fix for #339, but that was strictly for allowing click events to pass through to views that may be beneath a RiveAnimationView. This particular report is related to the regular setOnClickListener provided by Android being completely nonfunctional when used with a RiveAnimationView. Using the new riveTouchPassThrough property does not resolve this issue.

The RiveAnimationFixedView posted above does seem to work for this use case, but given this issue is a few years old I'm hoping this is something that can be fixed so that we don't need to rely on such workarounds.

Orbyt avatar Aug 19 '25 17:08 Orbyt