xamarin.androidwindowinsets

Xamarin (Android): Detect if the device has a display notch or not


I'm trying to detect if the device my app is running on has a notch or not.

This has what I need but I'm not been able to find the Xamarin equivalent for getDisplayCutout() in the WindowInset class.


Solution

  • I managed to 'solve' this issue by measuring the size of the status bar and comparing it against a known/safe threshold.

    Won't claim this to be the best solution to this question but it holds up against the devices I've tested so far.

            private const int __NOTCH_SIZE_THRESHHOLD = 40; //dp
    
            /// <summary>
            /// Device has a notched display (or not)
            /// </summary>
            public bool HasNotch
            {
                get
                {
    
                    // The 'solution' is to measure the size of the status bar; on devices without a notch, it returns 24dp.. on devices with a notch, it should be > __NOTCH_SIZE_THRESHHOLD (tested on emulator / S10)
                    int id = MainActivity.Current.Resources.GetIdentifier("status_bar_height", "dimen", "android");
                    if (id > 0)
                    {
                        int height = MainActivity.Current.Resources.GetDimensionPixelSize(id);
                        if (pxToDp(height) > __NOTCH_SIZE_THRESHHOLD)
                            return true;
                    }
                    return false;
                }
            }
            /// <summary>
            /// Helper method to convert PX to DP
            /// </summary>
            private int pxToDp(int px)
            {
                return (int)(px / Android.App.Application.Context.Resources.DisplayMetrics.Density);
            }