onTouch Event on Layout only registering one Touch per Click


onTouch Event on Layout only registering one Touch per Click



I have the following Problem: I created a new Class extending a ConstraintLayout and I am overriding the onTouchEvent function to animate the Layout.


@Override
public boolean onTouchEvent(MotionEvent event) {

switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(this, "scaleX", 0.85f);
ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(this, "scaleY", 0.85f);
scaleDownX.setDuration(100);
scaleDownY.setDuration(100);

AnimatorSet scaleDown = new AnimatorSet();
scaleDown.play(scaleDownX).with(scaleDownY);

scaleDown.start();
break;

case MotionEvent.ACTION_CANCEL:
Log.d("arne", "CANCEL");
// Beide Cases lösen das Event aus
// ACTION_CANCEL minimiert den Button auch, wenn man den Knopf nicht loslässt, sondern weg wischt
case MotionEvent.ACTION_UP:
ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(this, "scaleX", 1f);
ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(this, "scaleY", 1f);
scaleUpX.setDuration(100);
scaleUpY.setDuration(100);

AnimatorSet scaleUp = new AnimatorSet();
scaleUp.play(scaleUpX).with(scaleUpY);

scaleUp.start();
break;
}

return super.onTouchEvent(event);
}



This works flawlessly for Buttons and if I keep the button pressed down the Event fires multiple times in one Second.



But on an ConstraintLayout it fires only one time, just like the onClick Event.



This is a bit weired in my understanding.



Do you have a solution for this Problem?




1 Answer
1



Okay, I finally found the solution.



You just need to set your Layout clickable.


android:clickable="true"






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages