Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:02:40+00:00 2026-06-15T16:02:40+00:00

Trying to implement long press by borrowing code from GestureDetector I arrived at a

  • 0

Trying to implement long press by borrowing code from GestureDetector I arrived at a minimal sample which does not receive message in GestureHandler when onTouchEvent() returns true. When returning false, message does get delivered, but event processing ends and the long press does not get cancelled.

Is there a way to make this code work with onTouchEvent() returning true?

public class OverlayView extends View  {
    private static final int LONG_PRESS = 1;
    private Handler handler;
    private static final String TAG = OverlayView.class.getName();
    private class GestureHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case LONG_PRESS:
                dispatchLongPress();
                break;

            default:
                throw new RuntimeException("Unknown message " + msg);
            }
        }
    }

    public OverlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        handler = new GestureHandler();
    }

    private void dispatchLongPress() {
        Toast.makeText(getContext(), "Long Press", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        Log.d(TAG, e.toString());
        switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            handler.removeMessages(LONG_PRESS);          
            handler.sendEmptyMessageAtTime(LONG_PRESS, e.getDownTime() + 1000);
            break;
        case MotionEvent.ACTION_MOVE:
            handler.removeMessages(LONG_PRESS);
            break;
        case MotionEvent.ACTION_UP:
            handler.removeMessages(LONG_PRESS);
            break;
        default:
            break;
        }

        return true;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:02:42+00:00Added an answer on June 15, 2026 at 4:02 pm

    Even if you have a very steady hand you will likely create an ACTION_MOVE event:

    case MotionEvent.ACTION_MOVE:
        handler.removeMessages(LONG_PRESS);
        break;
    

    Your finger will only move a few pixels, but this is enough to remove your long press callback.

    Android uses a couple static variables label ___SLOP and calculates the distance between the first ACTION_DOWN event and the current event. Once the MotionEvent has traveled beyond the slop threshold, only then does it cancel the callback. I recommend using the same approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement Future.get(long, TimeUnit) in terms of TimeUnit.timedWait(Object, long) . It's not
I am trying to implement a WF4 activity extension which to handle some long
I'm trying to implement a Broadcast Receiver who should receive Intent from a Service
I'm trying to implement long polling technique using NodeJS. I have this basic code
I'm trying to implement an http long polling server in Node.js, and have no
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
I'm trying to implement long polling using Netty and jQuery. I have it working
I am trying to implement long polling in my Spring-MVC Web App but it
I'm trying to implement long division for bignums. I can't use a library like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.