0

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.

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
  • I havent worked with web views that much but a guess, you are loading the video url into the webview beforeit has any height. Try loading it after the dialog is open i.e move the line ```webView.LoadUrl(videoUrl);``` after ```dialogBuilder.Show();``` – Sayok Majumder Jul 10 '21 at 16:17
  • Hi @SayokMajumder, I moved all after the Show and nothing changed. Pretty much the same. – Federico Navarrete Jul 10 '21 at 16:32
  • I checked with the same code but its working [Screen shot](https://imgur.com/gallery/QszKvLe). It might be the html file that has some error. It would be great if we could have a look. – Sayok Majumder Jul 11 '21 at 04:57
  • Hi @Sajok, the HTML Only has an embedded video with height and width in 100% important everywhere. – Federico Navarrete Jul 11 '21 at 05:16

2 Answers2

0

At first , try to load a simple ,static html (e.g with some simply title) in AlertDialog.

And then try to create a new xml layout just with a webview inside , refer to https://stackoverflow.com/a/50334176/8187800 .

At last , if problem persists would you mind sharing a basic, minimal project here ?

ColeX
  • 14,062
  • 5
  • 43
  • 240
0

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.

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76