How to get safe area in android for notch devices

Amit Desale picture Amit Desale · Sep 12, 2019 · Viewed 7.4k times · Source

I need to handle layout for notch devices. I know in Ios. I can handle it using safe area in Ios. But in android is there any way to achieve this ?

Answer

Konstantin picture Konstantin · Jan 30, 2020

You need DisplayCutout object for this In yours Activity class (in onCreate()):

WindowManager.LayoutParams lp = this.getWindow().getAttributes();

lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;    

WindowInsets windowInsets = this.getWindow().getDecorView().getRootView().getRootWindowInsets();

DisplayCutout displayCutout = windowInsets.getDisplayCutout();
int bottom = displayCutout.getSafeInsetBottom()

more about this class you can find here: https://developer.android.com/guide/topics/display-cutout and here: https://blog.felgo.com/app-dev-tips-for-devices-with-edge-to-edge-screens-2020