Skip to content Skip to sidebar Skip to footer

Handling Touch Events In Android When Using Multiple Views/layouts

I'm very new to Android programming, and trying to understand touch events with nested views. To start, here's a description of my app: I have a relative layout that I've added via

Solution 1:

The way touch events are handled is kind of a cascading effect that starts from the top view and goes down to the lower nested views. Basically, Android will pass the event to each view until true is returned.

The general way you could implement the onTouchEvent event of a View would be:

@OverridepublicbooleanonTouchEvent(MotionEvent event) {
  booleanactionHandled=false;
  finalintaction= event.getAction();

  switch(action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
      // user first touches view with pointerbreak;
    case MotionEvent.ACTION_MOVE:
      // user is still touching view and moving pointer aroundbreak;
    case MotionEvent.ACTION_UP:
      // user lifts pointerbreak;
  }

  // if the action was not handled by this touch, send it to other viewsif (!actionHandled) 
     actionHandled |= super.onTouch(v, MotionEvent.event);

  return actionHandled;
}

Post a Comment for "Handling Touch Events In Android When Using Multiple Views/layouts"