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.
|