3

I am working on an Android app. I need to show a website on a webview and on an alert dialog. The site is shown in the webview, but not in the alert dialog. This is my code so far:

WebView:

WebView myWebView = (WebView) v.findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("https://...ilov_merged.html");

Alert Dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this,R.style.MyDialogTheme)
                        .setTitle("TERMS OF USE AND PRIVACY POLICY");

                WebView wv = new WebView(RegisterActivity.this);
                wv.loadUrl("https://...ilov_merged.html");
                wv.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);

                        return true;
                    }
                });
                builder.setView(wv);
                       builder .setCancelable(false);
                        builder.setPositiveButton(R.string.accept,
                                new Dialog.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialogInterface, int i) {
                                        // Mark this version as read.
                                        checkBox.setChecked(true);

                                        // Close dialog
                                        dialogInterface.dismiss();

                                        // Enable orientation changes based on
                                        // device's sensor

                                    }
                                })
                        .setNegativeButton(android.R.string.cancel,
                                new Dialog.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        // Close the activity as they have declined
                                        // the EULA


                                    }



                                });
                builder.create().show();

What is wrong in my code?

SCREENSHOT

enter image description here

mvasco
  • 4,965
  • 7
  • 59
  • 120
  • Just noticed in showing the `AlertDialog`, you aren't calling `settings.setJavaScriptEnabled`. Are you doing that? – Tim Kist Dec 16 '19 at 11:29

1 Answers1

1

Have you tried enabling javascript on the webview in your alertdialog? The page may not load without it, and your first approach enables it whilst your second doesn't. Since the URL has been removed, can't rule it out!

Additionally, I've experienced similar webview problems previously that were resolved by creating a new XML layout with just a webview inside, then inflating that for use:

    WebView webView = LayoutInflater.from(activity).inflate(R.layout.webview_fragment, null) as WebView
    webView.loadUrl("file:///android_asset/terms-register.html")
    AlertDialog.Builder(activity).setView(webView)

webview_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/VerticalScrollbar" />
Jake Lee
  • 7,549
  • 8
  • 45
  • 86
  • Thank you, I would like to try the first proposal, but inserting the line: wv.setJavaScriptEnabled(true); is not accepted and shown as error – mvasco May 14 '18 at 15:58
  • Hey, you need to take the code from your first approach, specifically the bit that handles `WebSettings`: `WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true);` Not certain this will help mind you, but it removes a possible cause. – Jake Lee May 14 '18 at 15:59
  • I have included a screenshot. Now the alert dialog opens with the the expected height, including title, blank space and buttons, but suddenly it become smaller and a gray stripe appears instead of the webview – mvasco May 14 '18 at 16:27
  • Can you try loading a simple, static html page? There may be some Javascript on your page that is resizing / adjusting viewpoint, causing the unexpected behaviour. – Jake Lee May 14 '18 at 16:29
  • Yes, I guess the problem lies on the web, I have tried another site and it is shown perfectly. – mvasco May 14 '18 at 16:31
  • Ah, okay. The page may be intentionally preventing you from embedding it unfortunately. – Jake Lee May 14 '18 at 16:32