Search code examples
androidxamarinxamarin.androidandroid-9.0-pie

Problem with LayoutInDisplayCutoutMode in Xamarin


I have a weird issue with setting an attribute for cutout mode in Xamarin.Android. I want to add support in my app for cutout mode, hence I updated the project to use SDK 9.0 and I added this single line to my Activity:

Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.ShortEdges;

My project compiles but when it runs I get this exception on the line above:

Java.Lang.NoSuchFieldError: no "I" field "layoutInDisplayCutoutMode" in class "Landroid/view/WindowManager$LayoutParams;" or its superclasses

My project settings are:

Compile using Android version: Android 9.0 (Pie)

Minimum Android version: Android 4.3 (API Level 18)

Target Android version: Android 9.0 (API Level 28)

All nuget libraries newest.


Solution

  • LayoutInDisplayCutoutMode was added in API level 28 (Pie).

    There is older device API support via the "new" AndroidX "Compat" libraries for cutout modes (androidx.core.view.DisplayCutoutCompat) but Microsoft/Xamarin still has not have published public support for them.

    There are numerous github-based issues surrounding this feature gap (for things like the new AndroidX's WorkManager and others I had to create my own binding...)

    For now you can a perform API level check,

    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.P)
    {
        Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.ShortEdges;
    }
    

    In Android P we added APIs to let you manage how your app uses the display cutout area, as well as to check for the presence of cutouts and get their positions.

    For devices running Android 8.1 (API 27), we've also back-ported the layoutInDisplayCutoutMode activity theme attribute so you can control the display of your content in the cutout area. Note that support on devices running Android 8.1 or lower is up to the device manufacturer, however.

    To make it easier to manage your cutout implementation across API levels, we've also added DisplayCutoutCompat in the AndroidX library, which is now available through the SDK manager.