Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is the order of operations for how Android handles touch events. Each touch event follows the pattern of (simplified example):</p> <ol> <li>Activity.dispatchTouchEvent()</li> <li>ViewGroup.dispatchTouchEvent()</li> <li>View.dispatchTouchEvent()</li> <li>View.onTouchEvent()</li> <li>ViewGroup.onTouchEvent()</li> <li>Activity.onTouchEvent()</li> </ol> <p><strong>But events only follow the chain until they are consumed</strong> (meaning somebody returns true from <code>onTouchEvent()</code> or a listener). In the case where you just touch somewhere on the screen, nobody is interested in the event, so it flows all the way down to your code. However, in the case of a button (or other clickable <code>View</code>) it consumes the touch event because it is interested in it, so the flow stops at Line 4.</p> <p>If you want to monitor all touches that go into your Activity, you need to override <code>dispatchTouchEvent()</code> since that what always gets called first, <code>onTouchEvent()</code> for an Activity gets called last, and only if nobody else captured the event. Be careful to not consume events here, though, or the child views will never get them and your buttons won't be clickable.</p> <pre><code>public boolean dispatchTouchEvent(MotionEvent event) { int eventaction=event.getAction(); switch(eventaction) { case MotionEvent.ACTION_MOVE: reg.setText("hey"); break; default: break; } return super.dispatchTouchEvent(event); } </code></pre> <p>Another option would be to put your touch handling code into a custom <code>ViewGroup</code> (like <code>LinearLayout</code>) and use its <code>onInterceptTouchEvent()</code> method to allow the parent view to steal away and handle touch events when necessary. Be careful though, as this interaction is one that cannot be undone until a new touch event begins (once you steal one event, you steal them all).</p> <p>HTH</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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