Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been debugging this for a really long time and it bothered me a lot since this came up very randomly. I then tested my code on different devices and realized the API implementation changed with Android 4.2. The expected behaviour and code work absolutely fine with Android 4.1.2 (tested on Galaxy Tab 2) but the bug you describe can be seen on a Nexus 7 (Android 4.2). Apparently Android changed the way MotionEvents are handled in API 17.</p> <p>One particular case when the bug does not occur is when the view is located in a <code>GroupLayout</code> under a <code>ScrollView</code>. When scrolling is possible the <code>ACTION_CANCEL</code> gets fired. However when no scrolling is possible the bug persists.</p> <p>At first I tried combining an <code>OnClickListener</code> and <code>OnTouchListener</code> so that the last can handle just the animations but to no avail. Dispatching the Events from parents also doesn't work.</p> <p>One workaround is to capture ACTION_MOVE events and check if the finger is located outside of the view's boundaries using <code>v.getX()</code> and <code>v.getY()</code> and comparing them to <code>event.getX()</code> and <code>event.getY()</code> respectably. A global boolean variable (<code>isOutside</code>) can be used to store the most current information. Before firing up the <code>ACTION_UP</code> you can check the latest state of <code>isOutside</code> and perform your animations and action accordingly. You can also return true or false depending on whether you captured the event or not.</p> <p>Update: After digging a bit here i found this solution: <a href="https://stackoverflow.com/questions/6410200/android-how-to-detect-if-use-touches-and-drags-out-of-button-region/8069887#8069887">Android: Detect if user touches and drags out of button region?</a> and compiled this code. The idea is the same except that it creates a rectangle and checks if the event boundaries are within the rectangle of the view.</p> <pre><code> someView.setOnTouchListener(new View.OnTouchListener() { private Rect rect; @Override public boolean onTouch(View v, MotionEvent event) { Log.d(TAG,"Touched: "+event.getAction()); if (event.getAction() == MotionEvent.ACTION_DOWN) { Log.d(TAG,"ACTION_DOWN"); animateImageButtonOnClick(v, event); rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); } if (event.getAction() == MotionEvent.ACTION_UP) { Log.d(TAG,"ACTION_UP"); if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { Log.d(TAG,"ACTION_UP - outside"); animateImageButtonOnRelease(v, event); } else { Log.d(TAG,"ACTION_UP - inside"); // do your stuff here } } if(event.getAction() == MotionEvent.ACTION_MOVE){ if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){ animateImageButtonOnReleaseWithLowDuration(v, event); } } if (event.getAction() == MotionEvent.ACTION_CANCEL){ Log.d(TAG,"ACTION_CANCEL"); animateImageButtonOnRelease(v, event); return true; } return true; } }); </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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