question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Webview] height 0 when we inspect with google chrome

See original GitHub issue

Description

I load one of my website into a webview on the main Page of my .net MAUI App

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <controls:CustomWebView x:Name="webView" Grid.Row="0">

        </controls:CustomWebView>
    </Grid>
    public class CustomWebView : WebView
    {
        public CustomWebView()
        {
#if __ANDROID__
            global::Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);// enable remote device in chrome
#endif
        }
    }

But my webapp inside the webview have a strange design. for example, height 100% are not respected load this simple html

<html style="height: 100%">
<body style="height: 100%">
<div style="height: 100%; width: 50px; background-color: red">
</div>
</body>
</html>

we actually see nothing in the webview, all elements have an height of 0px you Inspect the html node with chrome://inspect/#devices (debugging in android device with debugging usb enable) to verify it

when i load the webapp directly with chrome on the device, i don’t have this problem. Same when i install it with PWA

Steps to Reproduce

  1. Create a .NET Maui App
  2. put a webview in the main page with any url which exist
  3. deploy on targeting android physical device
  4. inspect the webview with chrome, you will see the height of the html at 0px (put height 100%, i will change nothing)

Version with bug

Preview 12 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11

Did you find any workaround?

no, except force the height with javascript in my webapp, but i don’t target all element so i still have strange design on layover, modals, this kind of stuff

document.querySelector("html").style.height =
                    document.querySelector("body").style.height =
                    document.querySelector(".app-loading").style.height =
                        window.innerHeight + "px";

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dmacgeocommented, Nov 23, 2022

If it can help anyone, I managed to patch this issue using the same approach as here: https://github.com/SKLn-Rad/Xam.Plugin.Webview/issues/11

Basically, you need to use a Android-specific webview handler and set the LayoutParameters property of your Android.Webkit.WebView to match the parent. For example :

var webView = new Android.Webkit.WebView(Context); 
webView.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
0reactions
mattleibowcommented, Dec 12, 2022

This is no longer an issue. Tested with net6 and net7 and it all renders fine and has the correct heights everywhere:

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image rendered in Android Webview with zero height
Upon inspecting the webview in Chrome, I got to know that the actual size (1080 x 1080) and rendered size (320 x 0)...
Read more >
1232745 - Android WebView returning 0 as its height after ...
Generally speaking, it works. However, the WebView from time to time still returns 0 as its height even if the content is fully...
Read more >
Pixel-Perfect UI in the WebView - Chrome Developers
There are a number of options you can use to create the perfect UI, this article will outline some of the best practices...
Read more >
WebView inside NestedScrollView has a messed up height ...
Open in chrome: chrome://inspect - You can see in the preview of the screen, that the webview has the full height of the...
Read more >
Managing WebView objects
Android provides several APIs to help you manage the WebView objects that display web content in your app. This page describes how to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found