View decorView = getWindow().getDecorView();
decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
DisplayCutout displayCutout = insets.getDisplayCutout();
if (displayCutout != null) {
// Display cutout is present
Log.d(TAG, "Display cutout detected");
Toast.makeText(MainActivity.this, "Detected", Toast.LENGTH_LONG).show();
} else {
// No display cutout
Log.d(TAG, "No display cutout detected");
Toast.makeText(MainActivity.this, " Not Detected", Toast.LENGTH_LONG).show();
}
}
return insets.consumeSystemWindowInsets();
}
I’m using the above code to detect if a cutout is present or not. But for the devices with cutout/ notch present it is still showing that there is no display cutout present.
2
Answers
Use the following code to get display cutouts (with approriate null checks):
You have to set layoutInDisplayCutoutMode first. Add this line before get getDisplayCutout