Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Opt out of edge to edge enforcement on api 35+ #25517

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Redth
Copy link
Member

@Redth Redth commented Oct 24, 2024

Android 35+ enforces apps extending "Edge to Edge" by default. See #24742 for details.

For now, a better compromise is to disable this by default in MAUI apps, until we can provide better support for edge to edge within .NET MAUI itself as well as better safe area insets on Android.

If you still want to manually make your app more compatible with edge to edge, you can disable this implicit opt out by including a similar (inverse) style:

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

And then applying it in the same way by calling it before the base.OnCreate(..) in your activity subclass (in the OnCreate method override):

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);
}

The new "Default" of opting out of the Edge to Edge enforcement on an Android 35+ device looks now like this:
Screenshot_1729819287

If you revert to the old behaviour by disabling the opt out, it will look like this:
Screenshot_1729819354

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);
}
```
@Redth Redth added this to the .NET 9.0 GA milestone Oct 24, 2024
@Redth Redth requested a review from a team as a code owner October 24, 2024 23:25
This will make it easier for devs to intentionally revert the implied opt-out of android 35 edge to edge enforcement behaviour that this PR also adds to MAUI.

Now all that's needed is to apply the disable style like this:

```csharp
protected override void OnCreate(Bundle? savedInstanceState)
{

	Theme?.ApplyStyle(Microsoft.Maui.Resource.Style.DisableOptOutEdgeToEdgeEnforcement, force: true);

	base.OnCreate(savedInstanceState);
}
```
@Redth Redth requested a review from PureWeen October 25, 2024 02:37
// 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);
Copy link
Member

@jonathanpeppers jonathanpeppers Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work to set this in Maui.MainTheme:

<item name="android:windowOptOutEdgeToEdgeEnforcement">true</item>

And just set it no matter what, probably doesn't do anything on API < 35?

This would avoid a C# -> Java call on startup.

Comment on lines +6 to +12
<style name="OptOutEdgeToEdgeEnforcement">
<item name="android:windowOptOutEdgeToEdgeEnforcement">true</item>
</style>

<style name="DisableOptOutEdgeToEdgeEnforcement">
<item name="android:windowOptOutEdgeToEdgeEnforcement">false</item>
</style>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you wouldn't even really need these styles.

@Redth Redth modified the milestones: .NET 9.0 GA, .NET 9 SR1 Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Ready To Review
Development

Successfully merging this pull request may close these issues.

2 participants