-1

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.

Jay Sidri
  • 6,271
  • 3
  • 43
  • 62

2 Answers2

0

There are some differences between native Android and Xamarin.Android .

In your case , the method getDisplayCutout() in Xamarin.Android is readonly property called DisplayCutout .

public DisplayCutout DisplayCutout { get; }

You could access it like

Android.Views.WindowInsets window = new WindowInsets(cpoySrc);
var Cutout = window.DisplayCutout;

cpoySrc is a source WindowInsets. Check https://learn.microsoft.com/en-us/dotnet/api/android.views.windowinsets.-ctor?view=xamarin-android-sdk-9

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • What is 'window'? Activity.Window? DisplayCutout was not found in there – Jay Sidri Feb 07 '20 at 01:55
  • Android.Views.WindowInsets . – Lucas Zhang Feb 07 '20 at 01:55
  • Here is a similar issue that you can check https://stackoverflow.com/questions/52814185/how-to-find-out-if-mobile-has-a-notch-or-not – Lucas Zhang Feb 07 '20 at 01:58
  • Should have mentioned it in my question that I tried that but WindowInset class does not actually have such a property - all I'm seeing are 4 methods (https://imgur.com/0kM2g6O) – Jay Sidri Feb 07 '20 at 02:00
  • 1
    Sorry, just got back to this - the answer does not make sense to me... where would I get a copy of the window insets to start with? And if I had one to begin with, why make another copy of it - i.e why not just read the DisplayCutout property from the copy object? Am I missing something obvious? – Jay Sidri Feb 10 '20 at 23:20
  • There are some differences between nativr Android and Xamarin.Android . Check https://stackoverflow.com/questions/54502680/problem-with-layoutindisplaycutoutmode-in-xamarin – Lucas Zhang Feb 14 '20 at 09:34
0

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);
        }
Jay Sidri
  • 6,271
  • 3
  • 43
  • 62