20

I want to load an HTML <IFRAME> inside an WebView, but I don't know why, it is not able to do so.

I am using following code to load <IFRAME>

webView.loadData("<iframe src=\"http://www.google.com\"></iframe>", "text/html",
                "utf-8");

Here's what I have tried.

WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setPluginsEnabled(true);
webViewSettings.setBuiltInZoomControls(true);
webViewSettings.setPluginState(PluginState.ON);

I have mentioned the internet permission:

<uses-permission android:name="android.permission.INTERNET" />

I have also tried settings the WebViewClient with shouldOverrideUrlLoading always returning false.

But it just isn't working.

I have tried this with diffrent sites, i.e. sites other than, google.com.

I am testing this on, Samsung Nexus S running ICS 4.0.3

Shardul
  • 27,760
  • 6
  • 37
  • 35

3 Answers3

13

Try with the below code:

webView.setInitialScale(1);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

Log.e(SimpleBillsConstants.SIMPLE_BILLS, width + "-" + height);

String data_html = "<!DOCTYPE html><html> <head> <meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"target-densitydpi=high-dpi\" /> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link rel=\"stylesheet\" media=\"screen and (-webkit-device-pixel-ratio:1.5)\" href=\"hdpi.css\" /></head> <body style=\"background:black;margin:0 0 0 0; padding:0 0 0 0;\"> <iframe style=\"background:black;\" width=' "+width+"' height='"+height+"' src=\""+VIDEO_URL+"\" frameborder=\"0\"></iframe> </body> </html> ";

webView.loadDataWithBaseURL("http://vimeo.com", data_html, "text/html", "UTF-8", null);
dakab
  • 5,379
  • 9
  • 43
  • 67
Vinayagam.D
  • 322
  • 3
  • 10
  • Just a question. If you do `setPluginState(WebSettings.PluginState.ON);` and after you also do `setPluginState(WebSettings.PluginState.ON_DEMAND);` the latter will override the former right? Or you set one or the other and not both. – Lennon Spirlandelli Dec 22 '16 at 17:17
  • https://developer.android.com/reference/android/webkit/WebSettings.PluginState This will help your question I think. But as you said we can use any one. – Vinayagam.D Jun 03 '20 at 02:27
  • I think this answer should be marked as correct answer because it worked perfectly well on my end – Serenity Emmanuel Jan 25 '22 at 17:20
11

This is how it worked out.

I noticed Log cat was throwing me

WebKit permission issue: EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up

To fix this, I had to add android:hardwareAccelerated="true" in <application> tag of Manifest.

I was experiencing this on ICS and its found that the same issue will occur post Honeycomb devices.

Hope this will help some one out.

Shardul
  • 27,760
  • 6
  • 37
  • 35
2

following hack worked for me for loading iframes in webview.Hope some one still might find it useful

    String webContent="your data to be loaded in webview"
if(webContent.contains("iframe")){
                Matcher matcher = Pattern.compile("src=\"([^\"]+)\"").matcher(webContent);
                matcher.find();
                String src = matcher.group(1);
                webContent=src;

                try {
                    URL myURL = new URL(src);
                    webView.loadUrl(src);

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }else {

                webView.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + webContent, "text/html", "UTF-8", null);}

        }
    }
Muahmmad Tayyib
  • 689
  • 11
  • 28