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.
I found that I can do the following:
So if I use the same example as my question
Rect(442, 0 - 998, 171)
, then the above will return:Now I can determine exactly where the
DisplayCutout
is, after convertingpx
todp
of course.