RiveAnimationView's setOnClickListener doesn't work
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!!!!!!!
}
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)
}
}
@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.