Concurrent onClick and onTouchEvent Events

I have a button with which I process the onClick handler.

However, I also need to respond to onTouchEvent events so that I can handle the hilighting buttons.

The problem is that if I return false from my onTouchEvent handler, it will call the onClick handler, but it will never give me onTouchEvent for the subsequent up event.

If I return "true" from onTouchEvent, I will get "up", but it will never send an onClick touch.

So what are my options?

  • Implement my own "click" processing inside onTouchEvent - this means that I will need to track movements inside and outside the button area, etc. It seems to be difficult.

  • I could use my button instead of a selector instead of a single image and assign different images to different states. The problem I am facing is that selectors are cumbersome for modifying images, which I need to do. (When my application is in different β€œmodes”, the button images for different states change. Again, this seems too complicated.

Is there a simple way to accomplish all this?

+4
source share
2 answers

If you need both click and touch events on the same screen, use the GestureDetector . You should study your listener's onShowPress method in detail .

+1
source

It seems your best bet will depend on option 2 above.

I suggest creating a few Button StateListDrawable selectors, and then programmatically switch between styles using Button.setBackgroundResource() .

+1
source

All Articles