How to handle touch outside the view in Android?

pixel picture pixel · Apr 22, 2013 · Viewed 25.9k times · Source

I've found implementation of "Undo Bar" used in Gmail application for Android. "UndoBar" is basically a View that is displayed on top of the layout.

Unfortunately it's not complete - it has no functionality of dismissing bar by touching screen outside the bar.

I've implemented FrameLayout that overrides onInterceptTouchEvent to handle bar dismissing but touching Action Bar does nothing.

Is there any way to handle such events from Action Bar?

Below there is an Image with "UndoBar"shown. What I want to achieve to handle touch in Action bar represented by red dot.

enter image description here

Answer

Ercan picture Ercan · May 13, 2013

Try to override dispatchTouchEvent of your activity.

dispatchTouchEvent(MotionEvent event){

     int x= event.getRawX();
     int y= event.getRawY();


         if(/*check bounds of your view*/){
          // set your views visiblity to gone or what you want. 
         }
      //for prevent consuming the event.
      return super().dispatchTouchEvent(event);    
}

update

dispatchTouchEvent(MotionEvent event){

     int x= event.getRawX();
     int y= event.getRawY();

      return super().dispatchTouchEvent(event)||[YourView].onTouch(event);    
}