Search code examples
androidgestureandroid-10.0

Gesture navigation: missing the setSystemGestureExclusionRects() method in the ViewCompat class


Android has the View.setSystemGestureExclusionRects() API to disable "back" gestures at the specified areas for Android 10. This API is added in the API level 29, so we must check the SDK version before using it:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
   view.systemGestureExclusionRects = listOf(...)
}

The official documents says, that the ViewCompat class also contains the setSystemGestureExclusionRects() method. It would allow to avoid checking the SDK version:

ViewCompat.setSystemGestureExclusionRects(...)

This method must be available starting from androidx.core:core:1.1.0-dev01. In my project I have the implementation 'androidx.core:core:1.1.0' dependency. The version "1.1.0" is later than "1.1.0-dev01", but the ViewCompat.setSystemGestureExclusionRects() method is missing. Why?


Solution

  • That looks like a documentation bug. You will find it in 1.2.0. For example, here is the ViewCompat from 1.2.0-rc01, the current latest version (as of 10 January 2020). It has the function that you seek... though it is pretty much just what your code snippet shows, only in Java:

    /**
     * Sets a list of areas within this view's post-layout coordinate space where the system
     * should not intercept touch or other pointing device gestures. <em>This method should
     * be called by {@link View#onLayout(boolean, int, int, int, int)} or
     * {@link View#onDraw(Canvas)}.</em>
     * <p>
     * On devices running API 28 and below, this method has no effect.
     *
     * @param rects A list of precision gesture regions that this view needs to function correctly
     * @see View#setSystemGestureExclusionRects
     */
    public static void setSystemGestureExclusionRects(@NonNull View view,
            @NonNull List<Rect> rects) {
        if (Build.VERSION.SDK_INT >= 29) {
            view.setSystemGestureExclusionRects(rects);
        }
    }
    

    FWIW, I filed a bug report about the documentation error.