Gestures - onFling() not called when longPressed is enabled

Put your problem here if it does not fit any of the other categories.

Gestures - onFling() not called when longPressed is enabled

Postby tsemann » Thu Jan 13, 2011 8:28 pm

Hello developers,

I have got a serious problem concerning the detection of gesture events.

I have implemented my own gesture detector and have the following problem:

When I pressed the touchscreen for a long time (onLongPress() is called) and I keep touching the screen my application doesn't listen to my touch events anymore. Do you maybe know why the touch events don't get detected anymore after keeping the finger on the screen?
I want to implement a swipe gesture after keeping the finger on the screen for a longer time (after onLongpress() is called). I hope you understood my problem :-) and here is my code:
Code: Select all
package de.berlin.magun.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class GestureDetect extends Activity implements OnGestureListener {
   private LinearLayout main;
   private TextView viewA;

   boolean longPress;

   private static final int SWIPE_MIN_DISTANCE = 120;
   private static final int SWIPE_MAX_OFF_PATH = 250;
   private static final int SWIPE_THRESHOLD_VELOCITY = 200;

   private GestureDetector gestureScanner;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      gestureScanner = new GestureDetector(this);
      main = new LinearLayout(this);
      main.setBackgroundColor(Color.GRAY);
      main.setLayoutParams(new LinearLayout.LayoutParams(320, 480));
      viewA = new TextView(this);
      viewA.setBackgroundColor(Color.YELLOW);
      viewA.setTextColor(Color.BLACK);
      viewA.setTextSize(16);
      viewA.setLayoutParams(new LinearLayout.LayoutParams(320, 80));
      main.addView(viewA);
      setContentView(main);
   }

   @Override
   public boolean onTouchEvent(MotionEvent me) {
      return gestureScanner.onTouchEvent(me);
   }

   @Override
   public boolean onDown(MotionEvent e) {
      viewA.setText("-" + "DOWN" + "-");
      return true;
   }

   @Override
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
         float velocityY) {
      try {
         if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
         // right to left swipe
         if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
               && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               Toast.makeText(getApplicationContext(), "Left Swipe",
                     Toast.LENGTH_SHORT).show();
         } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
               && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Right Swipe",
                  Toast.LENGTH_SHORT).show();
         } else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
               && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Swipe up",
                  Toast.LENGTH_SHORT).show();
         } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
               && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            Toast.makeText(getApplicationContext(), "Swipe down",
                  Toast.LENGTH_SHORT).show();
         }
      } catch (Exception e) {
         // nothing
      }
      return true;
   }
   
   

   @Override
   public void onLongPress(MotionEvent e) {
      Toast mToast = Toast.makeText(getApplicationContext(),"Long Press",
            Toast.LENGTH_SHORT);
       mToast.show();
   }

   @Override
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
         float distanceY) {
      viewA.setText("-" + "SCROLL" + "-");
      return true;
   }

   @Override
   public void onShowPress(MotionEvent e) {
      viewA.setText("-" + "SHOW PRESS" + "-");
   }

   @Override
   public boolean onSingleTapUp(MotionEvent e) {
      Toast mToast = Toast.makeText(getApplicationContext(), "Single Tap",
            Toast.LENGTH_SHORT);
      mToast.show();
      return true;
   }

}



Best regards and thank you,
tsemann
tsemann
Freshman
Freshman
 
Posts: 9
Joined: Thu Jan 13, 2011 8:19 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 27 guests