Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if device support notch display?

Tags:

android

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.

like image 299
Neeraj Gupta Avatar asked Sep 26 '18 09:09

Neeraj Gupta


People also ask

How do I know if my device has notch or not in flutter?

You can use MediaQuery. of(context). viewPadding and check whether the padding is greater than zero ,then you will need a safe area as there is a notch . And the best way to handle notch behaviour is to use the flutter_device_type package.

How do I enable notch on Android?

To enable it, locate the information for your phone and tap the build number seven times to enable them. After enabling the Developer options, open it and look for a setting called 'Simulate a display with a cutout'. Tap it, and from the menu that opens, select the kind of notch you want to add to your display.


2 Answers

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)
}
like image 74
Sachin Jagtap Avatar answered Oct 07 '22 05:10

Sachin Jagtap


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.

like image 24
sheltond Avatar answered Oct 07 '22 05:10

sheltond