reza asghari
reza asghari

Reputation: 43

Android: how to load html string inside a WebView?

I have a webview in my layout file in android app and I want to load a video with html and javascript inside it. the html string that i want to use to load the video is:

video= '<div id="15518031408206776"><script type="text/JavaScript" src="https://www.aparat.com/embed/gJ8jm?data[rnddiv]=15518031408206776&data[responsive]=yes"></script></div>'

the WebView xml code is:

<WebView
                        android:layout_width="match_parent"
                        android:layout_height="250dp"
                        android:layout_marginTop="30dp"
                        tools:ignore="WebViewLayout"
                        android:id="@+id/post_video" />

i also enable the javascriop in webview:

post_video.getSettings().setJavaScriptEnabled(true)

post_video.loadDataWithBaseURL( null, video, "text/html", "UTF-8", null );

but the webview just show the plain text html code instead of video!

you can see the result in this screenshot

how can i solve this problem?

Update:

I solved the problem by using Html.fromHtml(video) to decode my html string! now it's working and the WebView shows the video.

Upvotes: 2

Views: 7654

Answers (3)

Eric Yanush
Eric Yanush

Reputation: 404

Try using the loadData (String data, String mimeType, String encoding) method instead, with the "text/html" mime type and "UTF-8" encoding.

Also, you should be wrapping your html content with <html> and <body> tags, so you will have a valid html document instead of just a valid document fragment.

Upvotes: 1

Sam
Sam

Reputation: 5392

Simple. Make an adapter method like this.

  @JvmStatic
@BindingAdapter("htmlString")
fun loadHTML(view: WebView, htmlString: String) {
    if(htmlString.isEmpty()){
        return
    }

    try{
        view.webChromeClient = WebChromeClient()
        view.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
            }
            override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
                return false
            }
        }

        view.loadData(htmlString, "text/html", "UTF-8")
    }catch (ex: Exception){
        A35Log.e(TAG, "Error loading html string")
    }
}

Then use it in your xml code like this.

 <WebView
    android:id="@+id/wvHtml"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    bind:htmlString="@{viewModel.infoModel.hTML}"
    />

If you are not using data binding and you want to just use code, then simply take the code from the adapter example and load it directly into the webview. However, databinding and adapters are a very nice way to keep code clean and small.

Lastly, in pie it stopped allowing http traffic (clear traffic) to load by default. So if it fails to load for any reason, you may have to add to your application tag to allow non-secure html to load. I'm not sure if loading your own HTML string counts as "non-secure" or not, but it might.

 <application
    ...
   android:usesCleartextTraffic="true">

</application>

Upvotes: 0

Ravi
Ravi

Reputation: 930

Try adding below attributes to webview

    WebView webview = (WebView) findViewById(R.id.webView1);
    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);
    webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);

Also add android:hardwareAccelerated="true" to application tag in manifest file

Upvotes: 2

Related Questions