wayne piekarski Adding a DismissOverlayView and dispatchTouchEvent() to your Android Wear Activity

 
Share Blog article posted October 2014

Android Wear applications typically allow the user to swipe them away. However, when making an interactive application that requires full screen and usage of all swipe directions, you do not want the application to exit when you swipe right.

A nice way to exit your applications is to use DismissOverlayView. You simply add this to your view hierarchy, either in XML, or using some code like this to add it programmatically:

  mMainLayout = (RelativeLayout)findViewById(R.id.main_layout); // This is your existing top level RelativeLayout

  mDismissOverlayView = new DismissOverlayView(MyActivityWear.this);
  mMainLayout.addView(mDismissOverlayView,new RelativeLayout.LayoutParams(
                      RelativeLayout.LayoutParams.MATCH_PARENT,
                      RelativeLayout.LayoutParams.MATCH_PARENT));

Next, you need to declare a gesture detector, which is responsible for looking at incoming events, and deciding if they are a long-press or not. So if the gesture detector gets a long press, we need to show() the DismissOverlayView we just created. So you would add code like this to your onCreate() method in your activity:

   mGestureDetector = new GestureDetectorCompat(MyActivityWear.this, new GestureDetector.SimpleOnGestureListener(){
         @Override
         public void onLongPress (MotionEvent e){
           // Detected long press, showing exit widget
           mDismissOverlayView.show();
         }
   });

The final step is we need to register for touch events within the Activity. It is important that you override dispatchTouchEvent(), so that you get every event that happens on the display. If you try to override just onTouchEvent(), then you will not get all events if they are processed by other Views in the hierarchy. If you read the documentation for onTouchEvent() it mentions that this is only called when a touch screen event was not handled by any of the views under it. The documentation for dispatchTouchEvent() says that You can override this to intercept all touch screen events before they are dispatched to the window, so we should override that. Make sure you still call the original implementation via super.dispatchTouchEvent() if you want other Views to still see the touch event.

    @Override
    public boolean dispatchTouchEvent (MotionEvent e) {
        return mGestureDetector.onTouchEvent(e) || super.dispatchTouchEvent(e);
    }

And there you have it. Just paste in the above code snippets and you are good to have DismissOverlayView working in your code.


Share Blog article posted October 2014


Google Developer Advocate 2014-2023


X-Plane plugins and apps for flight simulation


IoT water meter monitoring


IoT computer vision monitoring


Tiny and cheap offline Wikipedia project 2017


Outdoor augmented reality research
Tinmith 1998-2007


Outdoor augmented reality 3D modelling
Tinmith 1998-2007


Outdoor augmented reality gaming
ARQuake 1999-2007


Scanned physical objects outdoors
Hand of God 3D 2006


Google Developer Advocate 2014-2023


X-Plane plugins and apps for flight simulation


IoT water meter monitoring


IoT computer vision monitoring


Tiny and cheap offline Wikipedia project 2017


Outdoor augmented reality research
Tinmith 1998-2007


Outdoor augmented reality 3D modelling
Tinmith 1998-2007


Outdoor augmented reality gaming
ARQuake 1999-2007


Scanned physical objects outdoors
Hand of God 3D 2006


Contact Wayne Piekarski via email wayne AT tinmith.net for more information

Last Updated 2024