Search code examples
androidxamarin.androidandroid-webviewandroid-alertdialog

How to fix the Height in a WebView inside an AlertDialog (fullscreen) that is always zero in Xamarin.Android?


I have been trying for a while to display one internal website in an AlertDialog.Builder. After a while, I found that the main issue is that it doesn't have a height because the website is being loaded:

How it looks:

error

How it looks after I add a height:

correct

These are my XMLs:

webview_fulscreen.xml

<?xml version="1.0" encoding="UTF-8" ?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frameFull"
    android:orientation="vertical">
    <include layout="@layout/previewer" />
    <ImageView
        android:layout_width="@dimen/close_dialog"
        android:layout_height="@dimen/close_dialog"
        android:src="@drawable/ic_close_black_48dp"
        android:clickable="true"
        android:layout_gravity="left"
        android:layout_marginTop="@dimen/close_dialog_margin_top"
        android:layout_marginLeft="@dimen/close_dialog_margin_left"
        android:tint="@color/colorLblBnd"
        android:id="@+id/previewClose"
        />
</FrameLayout>

previewer.xml

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ProgressBar
        android:id="@+id/indeterminateBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:indeterminate="true"
        android:max="100"
        android:layout_height="wrap_content"
    />
    <android.webkit.WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView" />
</LinearLayout>

This is how I call the AlertDialog:

string videoUrl = "file:///android_asset/website/video.html";

View dialogView = LayoutInflater.From(view.Context).Inflate(Resource.Layout.webview_fullscreen, null);

var webView = dialogView.FindViewById<WebView>(Resource.Id.webView);

var imageView = dialogView.FindViewById<ImageView>(Resource.Id.previewClose);

var pBar = dialogView.FindViewById<ProgressBar>(Resource.Id.indeterminateBar);

webView.SetWebChromeClient(new WebChromeClient());

webView.Settings.JavaScriptEnabled = true;
webView.Settings.AllowFileAccessFromFileURLs = true;
webView.Settings.DomStorageEnabled = true;

webView.LoadUrl(videoUrl);

using var dialogBuilder = new AlertDialog.Builder(view.Context, Android.Resource.Style.ThemeMaterialLightNoActionBarFullscreen);

dialogBuilder.SetView(dialogView);

dialogBuilder.Show();

I tried setting the Height via a script but it didn't work. Any idea how to get the height or calculate? Also, I tried to set in the RelativeLayout programmatically but it didn't work also.


Solution

  • After some time, I had another similar bug and created this crazy solution that can be used here also:

    https://stackoverflow.com/a/68409038/1928691

    However, if you have a better option, I'll be glad to hear it.