---
title: "Detect touch press vs long press vs movement?"  
description: "Detect touch press vs long press vs movement?"  
author: "Anonymous User"  
published: 2015-04-25  
updated: 2015-04-25  
canonical: https://www.mindstick.com/forum/23142/detect-touch-press-vs-long-press-vs-movement  
category: "android"  
tags: ["android"]  
reading_time: 4 minutes  

---

# Detect touch press vs long press vs movement?

I'm currently fiddling around with Android [programming](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming), but I have a small problem detecting different touch events, namely a normal touch press (press on the screen and release [right away](https://yourviews.mindstick.com/view/87928/help-your-children-avoid-internet-scams-right-away)), a [long press](https://www.mindstick.com/forum/23140/how-to-handle-capture-a-long-press-in-windows-store-apps) (touch the screen and hold the finger on it) and [movement](https://yourviews.mindstick.com/view/81374/black-lives-matter-movement-hijacked-by-american-leftist-hippies) (dragging on the screen).\
What I wanted to do is have an image (of a circle) on my screen which I can drag around. Then when I press it once (short/normal press) a Toast [comes up](https://answers.mindstick.com/qa/45369/if-you-search-worst-bollywood-actor-on-google-guess-whose-name-comes-up) with some basic information about it. When I long press it, an AlertDialog with a list comes up to select a different image (circle, rectangle or triangle).\
I made a custom View with my own **OnTouchListener** to detect the events and draw the image in **onDraw**. The **OnTouchListener.onTouch** goes something like this:\

```
// has a touch press started?private boolean touchStarted = false;// co-ordinates of imageprivate int x, y;public boolean onTouch(View v, MotionEvent event) {    int action = event.getAction();    if (action == MotionEvent.ACTION_DOWN) {        touchStarted = true;    }    else if (action == MotionEvent.ACTION_MOVE) {        // movement: cancel the touch press        touchStarted = false;        x = event.getX();        y = event.getY();        invalidate(); // request draw    }    else if (action == MotionEvent.ACTION_UP) {        if (touchStarted) {            // touch press complete, show toast            Toast.makeText(v.getContext(), "Coords: " + x + ", " + y, 1000).show();        }    }    return true;}
```

\

The problem is that the press doesn't quite work as expected, because when I casually touch the screen it also detects a tiny bit of movement and cancels the touch press and moves around the image instead.

I "hacked" around this a bit my introducing a new [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) "mTouchDelay" which I set to 0 on ACTION_DOWN, [increase](https://www.mindstick.com/articles/12959/are-you-struggling-to-increase-the-profit-of-your-liquor-store) in MOVE and if it's >= 3 in MOVE I execute my "move" code. But I have a feeling this isn't really the way to go.

I also haven't found out how to detect a long press. The culprit really is the MOVE which seems to always trigger.

For an example of what I roughly want, see the [Android application](https://www.mindstick.com/forum/33405/how-to-get-the-build-version-number-of-your-android-application) "DailyStrip": it shows an image of a comic strip. You can drag it if it's too large for the screen. You can tap it once for some [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) to pop-up and long press it for an options menu.

PS. I'm trying to get it to work on Android 1.5, since my phone only runs on 1.5.

## Replies

### Reply by Anonymous User

**From the Android Docs -**\
**onLongClick()**\
From View.OnLongClickListener. This is called when the user either touches and holds the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses and holds the suitable "enter" key or presses and holds down on the trackball (for one second).\
**onTouch()**\
From View.OnTouchListener. This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).\
As for the "moving happens even when I touch" I would set a delta and make sure the View has been moved by at least the delta before kicking in the movement code. If it hasn't been, kick off the touch code.\
**In the initialisation of your activity:**\
\

```
setOnLongClickListener(new View.OnLongClickListener() {  public boolean onLongClick(View view) {    activity.openContextMenu(view);      return true;  // avoid extra click events  }});setOnTouch(new View.OnTouchListener(){  public boolean onTouch(View v, MotionEvent e){    switch(e.getAction & MotionEvent.ACTION_MASK){      // do drag/gesture processing.     }    // you MUST return false for ACTION_DOWN and ACTION_UP, for long click to work    // you can return true for ACTION_MOVEs that you consume.     // DOWN/UP are needed by the long click timer.    // if you want, you can consume the UP if you have made a drag - so that after     // a long drag, no long-click is generated.    return false;  }});setLongClickable(true);
```


---

Original Source: https://www.mindstick.com/forum/23142/detect-touch-press-vs-long-press-vs-movement

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
