75

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?

NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • did you try `loadData` ? – njzk2 Dec 11 '12 at 08:48
  • you can put this html string to file res/values/strings.xml and create item for example `your html here` and then load it by `Html.fromHtml(getString(R.string.website))`, please check it if you have some time, because I would like to know if it works too ;) – deadfish Dec 11 '12 at 08:48
  • (what do you need base url for, anyway ?) – njzk2 Dec 11 '12 at 08:49
  • 1
    @Lumma : Html.fromHtml is not meant for use with webView. It returns a Spanned that for use with TextViews. – njzk2 Dec 11 '12 at 08:49

5 Answers5

159

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>";
Talha
  • 12,673
  • 5
  • 49
  • 68
skaya129
  • 1,744
  • 1
  • 11
  • 6
17

You also can try out this

   final WebView webView = new WebView(this);
            webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
M.Ganji
  • 818
  • 8
  • 13
12

read from assets html file

ViewGroup webGroup;
  String content = readContent("content/ganji.html");

        final WebView webView = new WebView(this);
        webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);

        webGroup.addView(webView);
0

I had the same requirement and I have done this in following way.You also can try out this..

Use loadData method

web.loadData("<p style='text-align:center'><img class='aligncenter size-full wp-image-1607' title='' src="+movImage+" alt='' width='240px' height='180px' /></p><p><center><U><H2>"+movName+"("+movYear+")</H2></U></center></p><p><strong>Director : </strong>"+movDirector+"</p><p><strong>Producer : </strong>"+movProducer+"</p><p><strong>Character : </strong>"+movActedAs+"</p><p><strong>Summary : </strong>"+movAnecdotes+"</p><p><strong>Synopsis : </strong>"+movSynopsis+"</p>\n","text/html", "UTF-8");

movDirector movProducer like all are my string variable.

In short i retain custom styling for my url.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Nitin Bathija
  • 800
  • 3
  • 12
  • 24
0

if you are looking for something in JetpackCompose this can help you:

@Composable 
fun HtmlTextVisualizerComponent(textFromData: String) {
val mimeType = "text/html"
val encoding = "UTF-8"

LazyColumn(
    modifier = Modifier
        .padding(
            start = 24.dp,
            end = 24.dp,
            top = 16.dp,
            bottom = 24.dp,
        ),
) {
    items(1) {
        val state = rememberWebViewState(
            url = "data:$mimeType;$encoding,$textFromData",
        )
        val navigator = rememberWebViewNavigator()
        WebView(
            state = state,
            modifier = Modifier.fillMaxSize(),
            navigator = navigator,
            onCreated = {
                it.settings.javaScriptEnabled = true
            },
        )
    }
  }
}

sure don't forget to add the dependency in your gradle:

 implementation "com.google.accompanist:accompanist-webview:0.30.1"