0

I'm having problems trying to make an element respond differently when there is action like "click" or type "touch". When I click on the element (is a simple listener, not worth paste the code), it performs the action perfectly, but completely ignores the action of touch. That way I could associate the element to distinguish the two events?

Should I apply the listener to the children of the view that the action is executed?

Thanks.

Edit

Resolved!

Added a check on the use of the touch event before calling it.

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    super.dispatchTouchEvent(event);
    if (this.onTouchEvent(event))
        return this.onTouchEvent(event);
    else
        return false;
}
Danilo
  • 3
  • 3

1 Answers1

1

You can refer this thread for help, or use the answer straight away. Good luck =)

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        // record the start time, start the timer
        mEventStartTime = ev.getEventTime();
        mHandler.postDelayed(mTask, LONG_PRESS_TIME);
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        // record the end time, dont show if not long enough
        mEventEndTime = ev.getEventTime();
        if (mEventEndTime - mEventStartTime < LONG_PRESS_TIME) {
            mHandler.removeCallbacks(mTask);        
        }
    } else {
        // moving, panning, etc .. up to you whether you want to
        // count this as a long press - reset timing to start from now
                    mEventStartTime = ev.getEventTime();
        mHandler.removeCallbacks(mTask);
                    mHandler.postDelayed(mTask, LONG_PRESS_TIME);
    }

    return super.onTouchEvent(ev);
}
Community
  • 1
  • 1
crazyPixel
  • 2,301
  • 5
  • 24
  • 48
  • I understand your code. But the program don't execute the method when I click on the buttons (with OnClickListener). – Danilo Sep 30 '13 at 17:20
  • can you post the whole class? I suspect your listener is set on the class rather than the button – crazyPixel Sep 30 '13 at 19:15