Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Android, attach guesture listener
    primarykey
    data
    text
    <p>I wrote <code>Gesture Detector class</code> and no try to add the gesture listener, it works fine when i call it in the <code>onCreate()</code> function for the <code>activity_main</code> view but i can't attach it to the invisible view with i create by button click.. <br>Here is my code,</p> <p>I receive no errors, it just don't listen, i can also set a <code>onTouchListener()</code> in the <code>invisibleView class</code>.. </p> <p>main</p> <pre><code>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"); } }); } } </code></pre> <p>here the class where i create the <code>system overlay view</code>:</p> <pre><code>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; } } } </code></pre> <p>here the my Class of Gesture Dedection:</p> <pre><code>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) &gt; Math.abs(diffY)) { if (Math.abs(diffX) &gt; SWIPE_THRESHOLD &amp;&amp; Math.abs(velocityX) &gt; SWIPE_VELOCITY_THRESHOLD) { if (diffX &gt; 0) { onSwipeRight(); } else { onSwipeLeft(); } } } else { if (Math.abs(diffY) &gt; SWIPE_THRESHOLD &amp;&amp; Math.abs(velocityY) &gt; SWIPE_VELOCITY_THRESHOLD) { if (diffY &gt; 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() { } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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