i have a html string containing this:
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="spanish press" content="spain, spanish newspaper, news,economy,politics,sports"> <title></title> </head> <body id="body"> <!-- The following code will render a clickable image ad in the page --> <script src="http://www.myscript.com/a"></script> </body> </html>
I need to show that website into a webview in android.
I tryed with all this:
webView.loadDataWithBaseURL(null, txt, "text/html", "UTF-8", null); webView.loadDataWithBaseURL("x-data://base", txt, "text/html", "UTF-8", null); webView.loadDataWithBaseURL("notreal/", txt, "text/htm", "utf-8",null);
Also i tryed removing DOCTYPE tag:
txt=txt.replace("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">", "");
No one of those have work. I just achieved to show the string into the webview (the html code), but not the website that must be created with that html code.
What is wrong?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.
you can put this html string to file res/values/strings. xml and create item for example <string name="website">your html here</string> and then load it by Html. fromHtml(getString(R. string.
The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.
To load your data in WebView. Call loadData() method of WebView
wv.loadData(yourData, "text/html", "UTF-8");
You can check this example
http://developer.android.com/reference/android/webkit/WebView.html
[Edit 1]
You should add -- \ -- before -- " -- for example --> name=\"spanish press\"
below string worked for me
String webData = "<!DOCTYPE html><head> <meta http-equiv=\"Content-Type\" " + "content=\"text/html; charset=utf-8\"> <html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=windows-1250\">"+ "<meta name=\"spanish press\" content=\"spain, spanish newspaper, news,economy,politics,sports\"><title></title></head><body id=\"body\">"+ "<script src=\"http://www.myscript.com/a\"></script>şlkasşldkasşdksaşdkaşskdşk</body></html>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With