21

Currently I am facing a problem to detect if mobile device support notch display in android.

Can any one help me in this? (I need to do this using code in android studio)

Thanks.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Neeraj Gupta
  • 323
  • 1
  • 2
  • 8
  • what have you tried so far? – Marcin Orlowski Sep 26 '18 at 09:14
  • Still searching a solution no idea how to do this – Neeraj Gupta Sep 26 '18 at 09:16
  • I don't remember any API that provides this information at all, I can only think of getting the device name or (any identity) that will identify the device and create a fix list of device that has notch and lastly compare the device name to your list, this might not cover all devices but it is a lot better that nothing at all. – Enzokie Sep 26 '18 at 09:24
  • Currently it is not possible it seems. Check this. https://stackoverflow.com/questions/50482390/how-can-we-get-the-size-of-screen-with-notch-on-the-top/50482572 – Remees M Syde Sep 26 '18 at 09:30

2 Answers2

21

Some Oreo devices also have notch display if you are targeting to support all OS then you can use my solution. As per material design guidelines the status bar height for Android devices is 24dp. You can get device status bar height and device density by using the following and check if status bar height is more than 24dp. If its height is more than 24dp then it has the notch on display and then you can handle your view position as per your requirement. This will work on Oreo as well.

int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
    statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}

// DP to Pixels

public static int convertDpToPixel ( float dp){
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return Math.round(px);
}

// Make UI adjustments as per your requirement

if (statusBarHeight > convertDpToPixel(24)) {
    RelativeLayout.LayoutParams topbarLp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    topbarlp.setMargins(0, statusBarHeight, 0, 0);

    //Set above layout params to your layout which was getting cut because of notch
    topbar.setLayoutParams(topbarlp)
}
Sachin Jagtap
  • 527
  • 6
  • 16
  • 1
    This is not working for infinity-O displays (samsung s10) – Pandiri Deepak Mar 07 '19 at 07:47
  • This solution is for detecting and handing notch display – Sachin Jagtap Mar 08 '19 at 07:03
  • 3
    Yes but these samsung s10 devices statusBarHeight is 24. We may need some generic solution. – Pandiri Deepak Mar 11 '19 at 07:36
  • @Pandiri Please check my answer. I am checking if status bar height is "Greater than 24dp". Because as per material design status bar height is 24 dp. If their is a notch then it gets increased. And samsung s10 has infinity display so it is different thing and it doesn't have notch so its height is 24dp. Infinity display need to handle in different way. – Sachin Jagtap Mar 11 '19 at 09:35
  • Use 25 instead of 24. – Ahamadullah Saikat Oct 14 '19 at 20:09
  • @AhamadullahSaikat do you found any device with 25dp status bar height and without knotch? – Sachin Jagtap Oct 16 '19 at 07:11
  • if in landscape mode, the "status_bar_height" seems to be 84, and even though the notch is not UI blocking the status_bar, the notch UI blocks the left or right sides of the app so need to also check for this scenario and make adjustments – BENN1TH Apr 10 '22 at 02:40
5

You need to use the getDisplayCutout method of the WindowInsets object. This will give you a DisplayCutout object, which you can query to find the "safe" insets within the display.

The DisplayCutout class is new in API level 28, so you won't be able to do it on devices with a lower API level (you can assume that there's no notch unless it's API level >= 28).

There's a guide here.

You can get hold of the WindowInsets object by overriding the onApplyWindowInsets method of the View, or by implementing a OnApplyWindowInsetsListener class.

sheltond
  • 1,877
  • 11
  • 15