4

I have a kids drawing app, but the gestures in Q keep quitting the app. I tried removing the system gesture but it does not seem to work.

In this case, I am trying to exclude the whole screen from system gesture:

List<Rect> exclusionRects = new ArrayList();

public void onLayout(boolean changedCanvas, int left, int top, int right, int bottom) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        exclusionRects.clear();
        exclusionRects.add(new Rect(left, top, right, bottom));
        setSystemGestureExclusionRects(exclusionRects);
    }
}
Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88

2 Answers2

4

As stated by Google:

First, to ensure reliable and consistent operation, there’s a 200dp vertical app exclusion limit for the Back gesture.

https://android-developers.googleblog.com/2019/08/final-beta-update-official-android-q.html

This means that the operating system will not allow you to override the back gesture fully.

This makes sense as it is a fairly fundamental part of the operating system and they probably don't want to allow apps that remove the gesture entirely, as it is bad for consistency across the platform

cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • What does "200dp vertical app exclusion limit" mean? Isn't the back gesture is on the left (horizontal)? – Lim Thye Chean Aug 13 '19 at 08:09
  • 1
    The back gesture is on both the left and right sides, and these are both vertical edges (vertical = up <-> down, horizontal = left <-> right). This means that of the full height of the screen, only 200dp are allowed to be excluded from gesture navigation. I.e. if your screen is 1000dp high, a minimum of 800dp will be used for the gesture no matter what. – cameron1024 Aug 13 '19 at 08:12
  • _"This makes sense"_ - except that now every drawing app is fundamentally broken because you can't draw on the left/right side of the screen without navigating? wtf?? – BlueRaja - Danny Pflughoeft Mar 26 '23 at 22:15
  • The three-button navigation mode still exists, but IMO it's a worthwhile tradeoff. I disagree that having a few pixels on either edge of the screen unusable "fundamentally breaks" drawing apps. And even if it does, I'd rather have this than allow apps that I download from the internet (without vetting source code) to disable navigating away from the app. – cameron1024 Mar 27 '23 at 12:06
0

Try this.

  1. Define this code in your Utils class.

     static List<Rect> exclusionRects = new ArrayList<>();
     public static void updateGestureExclusion(AppCompatActivity activity) {
       if (Build.VERSION.SDK_INT < 29) return;
       exclusionRects.clear();
       Rect rect = new Rect(0, 0, SystemUtil.dpToPx(activity, 16), getScreenHeight(activity));
       exclusionRects.add(rect);
    
       activity.findViewById(android.R.id.content).setSystemGestureExclusionRects(exclusionRects);
     }
    
     public static int getScreenHeight(AppCompatActivity activity) {
     DisplayMetrics displayMetrics = new DisplayMetrics();
     activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
     int height = displayMetrics.heightPixels;
     return height;
     }
    
     public static int dpToPx(Context context, int i) {
       return (int) (((float) i) * context.getResources().getDisplayMetrics().density);
     }
    
  2. Check if your layout is set in that activity where you want to exclude the edge getures and then apply this code.

     // 'content' is the root view of your layout xml.
     ViewTreeObserver treeObserver = content.getViewTreeObserver();
     treeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
         @Override
         public void onGlobalLayout() {
             content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
             SystemUtil.updateGestureExclusion(MainHomeActivity.this);
         }
     });
    
  • We are adding the view width to 16dp to trigger the code when user swipe right from left edge & height to screen height to do it fully left side.
Dev4Life
  • 2,328
  • 8
  • 22
  • This code works for the whole left edge, for bottom & right edge, you just need to add Rects in the arraylist like written above. – Dev4Life Dec 18 '20 at 04:00