Skip to content

Commit

Permalink
Android: Opt out of edge to edge enforcement on api 35+
Browse files Browse the repository at this point in the history
See #24742 for details.

It seems to be a better compromise to disable this by default in MAUI apps.  You can still opt out by including a similar style:

```xml
<style name="DisableOptOutEdgeToEdgeEnforcement">
   <item name="android:windowOptOutEdgeToEdgeEnforcement">false</item>
</style>
```

And then applying it in a similar way, either by calling it before the `base.OnCreate(..)` in your activity subclass (in the OnCreate method override), or by registering for the ActivityOnCreate lifecycle callback.

Eg:

```csharp
protected override void OnCreate(Bundle? savedInstanceState)
{
    // Disable Edge to Edge opt out by calling this before base.OnCreate()
    Theme?.ApplyStyle(Resource.Style.DisableOptOutEdgeToEdgeEnforcement, force: true);

    base.OnCreate(savedInstanceState);
}
```
  • Loading branch information
Redth committed Oct 24, 2024
1 parent d541b54 commit a5b47ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Core/src/Platform/Android/MauiAppCompatActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public partial class MauiAppCompatActivity : AppCompatActivity

protected override void OnCreate(Bundle? savedInstanceState)
{
// https://github.com/dotnet/maui/issues/24742
// By default we opt out of edge to edge enforcment on Android 35+
// Must be applied before the DecorView is setup
// We set force to false in case the style has already been explicitly
// applied to disable opting out of edge to edge enforcement
Theme?.ApplyStyle(Resource.Style.OptOutEdgeToEdgeEnforcement, force: false);

if (!AllowFragmentRestore)
{
// Remove the automatically persisted fragment structure; we don't need them
Expand Down
10 changes: 10 additions & 0 deletions src/Core/src/Platform/Android/Resources/values-v35/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<resources>

<!-- Opts out of edge to edge enforcement on API 35+ -->
<!-- https://github.com/dotnet/maui/issues/24742 -->
<style name="OptOutEdgeToEdgeEnforcement">
<item name="android:windowOptOutEdgeToEdgeEnforcement">true</item>
</style>

</resources>
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Android/Resources/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<resources>

<!-- Empty Matching style for values-v35/styles.xml -->
<style name="OptOutEdgeToEdgeEnforcement">
</style>

<!-- Base application theme. -->
<style name="Maui.MainTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/colorPrimary</item>
Expand Down

0 comments on commit a5b47ed

Please sign in to comment.