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

Keyboard overlaps WebView in MAUI #28790

Open
paulguz-datapa opened this issue Apr 3, 2025 · 3 comments
Open

Keyboard overlaps WebView in MAUI #28790

paulguz-datapa opened this issue Apr 3, 2025 · 3 comments
Labels
area-controls-webview WebView area-keyboard Keyboard, soft keyboard t/bug Something isn't working
Milestone

Comments

@paulguz-datapa
Copy link

Description

I've created this bug to essentially reopen #14197, as it appears it was closed without actually being fixed.

In the comments of the above bug, a user posted a workaround, which seems to work for most. It does not work for me - it actually seems to break the sizing of the WebView so that it expands beyond the boundaries of the screen.

Regardless of the above workaround, the bug was added to the MAUI SDK Ongoing queue, but then removed as it appears a bot mistook the workaround for an actual fix, and closed the issue.

I can confirm the issue remains in .Net 9.

As the workaround does not work for me, please reconsider fixing this properly.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

9.0.50 SR5

Is this a regression from previous behavior?

No, this is something new, Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

@paulguz-datapa paulguz-datapa added the t/bug Something isn't working label Apr 3, 2025
Copy link

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

@sheiksyedm sheiksyedm added the s/needs-repro Attach a solution or code which reproduces the issue label Apr 3, 2025
@Yu-Core
Copy link

Yu-Core commented Apr 7, 2025

The method I am currently using (.NET 9)
It can work, but there may be issues

using Android.Widget;
using static Android.Resource;
using Activity = Android.App.Activity;
using Rect = Android.Graphics.Rect;
using View = Android.Views.View;
using Android.App;
using AndroidX.Core.View;

namespace MauiBlazor
{
#nullable disable
    public class SoftKeyboardAdjustResize
    {
        Activity _activity;
        bool _edgeToEdge;
        FrameLayout.LayoutParams frameLayoutParams;
        int usableHeightPrevious = 0;
        Rect rect = new();
        View mChildOfContent;

        public SoftKeyboardAdjustResize(Activity activity, bool edgeToEdge = true)
        {
            _activity = activity;
            _edgeToEdge = edgeToEdge;
            mChildOfContent = _activity.FindViewById<FrameLayout>(Id.Content).GetChildAt(0);
            mChildOfContent.ViewTreeObserver.GlobalLayout += (s, e) => PossiblyResizeChildOfContent();
            frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent?.LayoutParameters;
            SetBackgroundColor(Android.Graphics.Color.White);
        }

        public void OnStop()
        {
            mChildOfContent.RequestLayout();
        }

        void PossiblyResizeChildOfContent()
        {
            _activity.FindViewById<FrameLayout>(Id.Content).GetWindowVisibleDisplayFrame(rect);
            var usableHeightNow = rect.Height();
            if (usableHeightNow != usableHeightPrevious)
            {
                int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
                int heightDifference = usableHeightSansKeyboard - usableHeightNow;
                if (_edgeToEdge)
                {
                    frameLayoutParams.Height = usableHeightNow + _activity.GetStatusBarInsets().Top + _activity.GetNavigationBarInsets().Bottom;
                    if (heightDifference > usableHeightSansKeyboard / 4)
                    {
                        //Resolve anomalies during screen rotation
                        mChildOfContent.RootView.Top = -_activity.GetStatusBarInsets().Top;
                        mChildOfContent.Layout(rect.Left, rect.Top, rect.Right, rect.Bottom);
                    }
                }
                else
                {
                    frameLayoutParams.Height = usableHeightNow;
                }

                mChildOfContent.RequestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }

        void SetBackgroundColor(Android.Graphics.Color color)
        {
            mChildOfContent.RootView.SetBackgroundColor(color);
        }
    }

    public static class ActivityExtensions
    {
        private static readonly AndroidX.Core.Graphics.Insets emptyInsets = AndroidX.Core.Graphics.Insets.Of(0, 0, 0, 0);

        public static AndroidX.Core.Graphics.Insets GetStatusBarInsets(this Activity activity)
        {
            Android.Views.View decorView = activity.FindViewById(Id.Content);
            WindowInsetsCompat windowInsets = ViewCompat.GetRootWindowInsets(decorView);
            if (windowInsets is null)
            {
                return emptyInsets;
            }

            return windowInsets.GetInsets(WindowInsetsCompat.Type.StatusBars());
        }

        public static AndroidX.Core.Graphics.Insets GetNavigationBarInsets(this Activity activity)
        {
            Android.Views.View decorView = activity.FindViewById(Id.Content);
            WindowInsetsCompat windowInsets = ViewCompat.GetRootWindowInsets(decorView);
            if (windowInsets is null)
            {
                return emptyInsets;
            }

            return windowInsets.GetInsets(WindowInsetsCompat.Type.NavigationBars());
        }
    }
}

Platforms/Android/MainActivity.cs

private SoftKeyboardAdjustResize softKeyboardAdjustResize;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    softKeyboardAdjustResize = new SoftKeyboardAdjustResize(this);
}

protected override void OnStop()
{
    base.OnStop();

    softKeyboardAdjustResize.OnStop();
}

If you not use EdgeToEdge

softKeyboardAdjustResize = new SoftKeyboardAdjustResize(this,false);

@dotnet-policy-service dotnet-policy-service bot added s/needs-attention Issue has more information and needs another look and removed s/needs-repro Attach a solution or code which reproduces the issue labels Apr 7, 2025
@rmarinho rmarinho added this to the Backlog milestone Apr 8, 2025
@rmarinho rmarinho added area-controls-webview WebView area-keyboard Keyboard, soft keyboard and removed s/needs-attention Issue has more information and needs another look labels Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-webview WebView area-keyboard Keyboard, soft keyboard t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants