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 ?
How to get safe area in android for notch devices
14.5k Views Asked by Amit Desale At
3
There are 3 best solutions below
1

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
1

this will give a Rect with the safe area in top, bottom, left and right. It also has API compatibility check
public static Rect getSafeArea(@NonNull Activity activity) {
final Rect safeInsetRect = new Rect();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
return safeInsetRect;
}
final WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
if (windowInsets == null) {
return safeInsetRect;
}
final DisplayCutout displayCutout = windowInsets.getDisplayCutout();
if (displayCutout != null) {
safeInsetRect.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
}
return safeInsetRect;
}
You can use this:
or the flag
with styles as