Identify the display cut out type applied Android 9.0-ThrowExceptions

Exception or error:

I am trying to identify the display cutout type selected in a programmatic way in android with OS >=9.0.

I tried this way

DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();

But display cutout always return null.

Also I am tried it in another way too

 int mode = ((Activity) context).getWindow().getAttributes().layoutInDisplayCutoutMode;

But this gives me the value of what my application support . I have set to never in my style.

But I want to know whenever user selects different cutout type from the developer options from setting.

Please let me know if this can be achieved!

How to solve:

After some tests, only the LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES works, after added an OnApplyWindowInsetsListener

I have successfully retrieved the DisplayCutout and is Rects

@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
    DisplayCutout cut = insets.getDisplayCutout();
    if(cut != null) {
        int space = insets.getDisplayCutout().getBoundingRects().get(0).height();
        HCNXLog.e(TAG, "DisplayCutout.. " + space);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
        lp.topMargin = space;
        toolbar.setLayoutParams(lp);
    }
    return insets;
}

Leave a Reply

Your email address will not be published. Required fields are marked *