0

I wrote Gesture Detector class and no try to add the gesture listener, it works fine when i call it in the onCreate() function for the activity_main view but i can't attach it to the invisible view with i create by button click..
Here is my code,

I receive no errors, it just don't listen, i can also set a onTouchListener() in the invisibleView class..

main

public class MainActivity extends Activity {

    String TAG = "myTAG";
    SocketClass mySocketClass;
    View viewToWatch;
    InvisibleView myInvisibleView;
    Context myContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
           savedInstanceState = new Bundle();
           myInvisibleView = new InvisibleView();

           /* 
          ----------------------------------------------------- 
           this works, when i add the listener to the main view:
          ----------------------------------------------------- 

           viewToWatch= (View) findViewById(R.id.mainViewId);
           viewToWatch.setOnTouchListener(new ClassGesturesDetection() {

               public void returnSingleTapUp() {
                    Log.d ("Gesture from main View", "single tab");
                }              
            });
        ----------------------------------------------------- 
        */ 

    }


  public void startApp(View activity_main_view){
        myInvisibleView.onCreate(this.getApplicationContext());
        putOnTochListenerToInvsibleView();
    }


    public void putOnTochListenerToInvsibleView( ) {

        /* 
          ----------------------------------------------------- 
           there i want to add the listener:
          ----------------------------------------------------- 
        */
         setContentView(R.layout.invisibleviewxml);
           viewToWatch= (View) findViewById(R.id.invisibleViewId);
           viewToWatch.setOnTouchListener(new ClassGesturesDetection() {
                   public void returnSingleTapUp() {
                    Log.d ("Gesture from transparent view", "single tab");
                }
            });
    }
}

here the class where i create the system overlay view:

public class InvisibleView extends Service {

    View myView;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    public void onCreate(Context myContext) {
       super.onCreate();

       LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
       myView = new View(myContext);
       myView = inflater.inflate(R.layout.invisibleviewxml, null);

       WindowManager.LayoutParams params = new WindowManager.LayoutParams(
               WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
               WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
               PixelFormat.TRANSLUCENT);
       WindowManager wm = (WindowManager) myContext.getSystemService(WINDOW_SERVICE);
        wm.addView(myView, params);

          /* 
          ----------------------------------------------------- 
           this also works,the system overlay catches clicks
          ----------------------------------------------------- 
        myView.setOnTouchListener( new OnTouchListener() {
            @Override
            public boolean onTouch(View inviView, MotionEvent event) {
                Log.d("Gesture",  "simple touch from InvisibleView Class");
                return true;
            }

        });
          ----------------------------------------------------- 
           this does not work
          -----------------------------------------------------  

          myView.setOnTouchListener(new ClassGesturesDetection() {
       public void returnSingleTapUp() {
            Log.d ("Gesture from transparent", "single tab");
        }

    });
          -----------------------------------------------------  */

    }

  @Override
    public void onDestroy() {
        super.onDestroy();
        if(myView != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(myView);
            myView = null;
        }
    }

}

here the my Class of Gesture Dedection:

public class ClassGesturesDetection implements OnTouchListener {

     @SuppressWarnings("deprecation")
        private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

        public boolean onTouch(final View view, final MotionEvent motionEvent) {
            return gestureDetector.onTouchEvent(motionEvent); 
        }

        private final class GestureListener extends SimpleOnGestureListener {

            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;

            @Override
            public boolean onDown(MotionEvent e) {
                //Log.d("class Gesture", "on Down");
                return true;
            }

            @Override
          public void onLongPress(MotionEvent e) {
                returnOnLongPress();
             }

            @Override
          public boolean onSingleTapUp(MotionEvent e) {  
                returnSingleTapUp();
                return false;
          }



            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                boolean result = false;
                try {
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                        }
                    } else {
                        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffY > 0) {
                                onSwipeBottom();
                            } else {
                                onSwipeTop();
                            }
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
        }

        public void onSwipeRight() {
        }

        public void onSwipeLeft() {
        }

        public void onSwipeTop() {
        }

        public void onSwipeBottom() {
        }

        public void returnSingleTapUp() {
        }
        public void returnOnLongPress() {
        }
    }
miholzi
  • 922
  • 1
  • 14
  • 36

1 Answers1

0

you must add this permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

You can follow this I had answered there for your question

Community
  • 1
  • 1
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
  • thanks, i have already added, the problem is not the system alert view, the problem is to add the guesture listener to this view – miholzi Nov 25 '13 at 10:08
  • yeah i do also had this problem once follow the link – Viswanath Lekshmanan Nov 25 '13 at 10:09
  • thanks, but i think the problem is not that i can't receive any touchevents from the system overlay, that works. I think the problem is this line: viewToWatch.setOnTouchListener(new ClassGesturesDetection() – miholzi Nov 25 '13 at 12:03