32

In Android 10, users can enable full screen gesture mode. I want to detect whether the device in full screen gesture mode or not. I can't find anything in documentation. How to do it programmatically at run time?

Java or kotlin language answer is OK.

Any official API or workaround...

iCantC
  • 2,852
  • 1
  • 19
  • 34
Jaya Prakash
  • 549
  • 6
  • 17

3 Answers3

24

You can use below code to check gesture or navigation mode

    public static int isEdgeToEdgeEnabled(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("config_navBarInteractionMode", "integer", "android");
        if (resourceId > 0) {
            return resources.getInteger(resourceId);
        }
        return 0;
    }

The value that returned by isEdgeToEdgeEnabled function will follow below:

  • 0 : Navigation is displaying with 3 buttons

  • 1 : Navigation is displaying with 2 button(Android P navigation mode)

  • 2 : Full screen gesture(Gesture on android Q)

Trung Đoan
  • 643
  • 7
  • 18
  • 2
    Is using resource identifier reliable? I read that they vary depends on device & may change over time... – Jaya Prakash Mar 18 '20 at 11:59
  • I found the resource identifier on android Q source code of google. so I think that OEMs will use same id for full screen gesture. – Trung Đoan Mar 19 '20 at 11:36
  • 3
    Important! You need to surround `resources.getInteger()` with try/catch to handle Resources.NotFoundException, otherwise this code will crash on various Xiaomi and Samsung devices. Also this code didn't work on Huawei devices. – Dmide Apr 06 '21 at 10:56
  • In my case (S21 Ultra, Android 11) config_navBarInteractionMode = 17694897, so this method is not very reliable – Dr. Failov Apr 29 '21 at 17:03
  • 2
    this is not working. I tried on xioami device. it both gives 0 in gesture and menu buttons mode – Abdullah Jun 03 '21 at 10:56
  • Unfortunately not working on Android T (Pixel 6a) – Alexey Ozerov Sep 30 '22 at 06:48
2

I found this article really useful in explaining what WindowInsets are and how to use them.

Basically I check if the left gesture inset is greater than 0, and if it is then the system is using gesture type navigation. Left and right gesture insets have to be greater than 0 in gesture type navigation because you swipe from the right or left to go back.

int gestureLeft = 0;

if (Build.VERSION.SDK_INT >= 29) {
    gestureLeft = this.getWindow().getDecorView().getRootWindowInsets().getSystemGestureInsets().left;
}

if (gestureLeft == 0) {
    // Doesn't use gesture type navigation
} else {
    // Uses gesture type navigation
}

Obviously, the window has to be rendered for this to work. You can add it inside an OnApplyWindowInsetsListener if you want this to run as soon as the window is rendered.

Note: I tried using getSystemGestureInsets().bottom, but it returned a non-zero value even when I wasn't using gesture type navigation.

Sujit
  • 1,653
  • 2
  • 9
  • 25
-1

According to docs, there is a hasInsets() method that returns true if the WindowInsets has any nonzero insets.
https://developer.android.com/reference/android/view/WindowInsets#hasInsets()

We can use it this way

view.rootWindowInsets.hasInsets()

Hope this helps!

Rami Jemli
  • 2,550
  • 18
  • 31