DisplayCutout position

1k views Asked by At

I'm trying to determine the position of the DisplayCutout(notch).

The Android Developer Blog, state the following:

In Android P we added APIs to let you manage how your app uses the display cutout area, as well as to check for the presence of cutouts and get their positions.


So I tried getting the position of the cutout, this is the only way I was able to get it:

if (SDK_INT >= Build.VERSION_CODES.P) {
    DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
    if (displayCutout != null) {
        List<Rect> bounding = displayCutout.getBoundingRects();
        for (int i=0; i<bounding.size(); i++) {
            Log.e("BoundingRect - ", ""+bounding.get(i));
        }                    
    }
}

When running the above on a Google Pixel 3XL, it returns Rect(442, 0 - 998, 171).

From the tests I've done, this correlates to:

442 - Where the cutout starts (on the x-axis), 442px from the left.
0 - Where the cutout starts (on the y-axis), 0px from the top.
998 - Where the cutout ends (on the x-axis), 998px from the left.
171 - Where the cutout ends (on the y-axis), 171px from the top.


My question: Since the DisplayCutout API doesn't return the positions/coordinates separately, what is the best way to get the positions/coordinates from the String<Rect>?

The only way I could think of is using String's substring, but this feels "hackish"/incorrect.

1

There are 1 answers

0
HB. On BEST ANSWER

I found that I can do the following:

if (SDK_INT >= Build.VERSION_CODES.P) {
    DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
    if (displayCutout != null) {
        List<Rect> bounding = displayCutout.getBoundingRects();
        for (int i=0; i<bounding.size(); i++) {
            int left = bounding.get(i).left;
            int right = bounding.get(i).right;
            int top = bounding.get(i).top;
            int bottom = bounding.get(i).bottom;
            Log.e("BoundingLeft - ", ""+left);
            Log.e("BoundingRight - ", ""+right);
            Log.e("BoundingTop - ", ""+top);
            Log.e("BoundingBottom - ", ""+bottom);
        }                    
    }
}

So if I use the same example as my question Rect(442, 0 - 998, 171), then the above will return:

E/BoundingLeft -: 442
E/BoundingRight -: 998
E/BoundingTop -: 0
E/BoundingBottom -: 171

Now I can determine exactly where the DisplayCutout is, after converting px to dp of course.