Note that there are some explanatory texts on larger screens.

plurals
  1. PODetect touch press vs long press vs movement?
    text
    copied!<p>I'm currently fiddling around with Android programming, but I have a small problem detecting different touch events, namely a normal touch press (press on the screen and release right away), a long press (touch the screen and hold the finger on it) and movement (dragging on the screen).</p> <p>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 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).</p> <p>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:</p> <pre><code>// has a touch press started? private boolean touchStarted = false; // co-ordinates of image private 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; } </code></pre> <p>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.</p> <p>I "hacked" around this a bit my introducing a new variable "mTouchDelay" which I set to 0 on ACTION_DOWN, increase 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.</p> <p>I also haven't found out how to detect a long press. The culprit really is the MOVE which seems to always trigger.</p> <p>For an example of what I roughly want, see the 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 to pop-up and long press it for an options menu.</p> <p>PS. I'm trying to get it to work on Android 1.5, since my phone only runs on 1.5.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload