I'm working on a project where I'm receiving some html string which I should show in WebView. The problem is that the html string contains only body part, without any tags like <html>, <head>, <body> and when I'm trying to show the received string in webView the result is showing the tags included in html string received from server.
Here is some example what I receive and what I'm doing :
Html String received from server :
05-30 14:02:56.785: D/(16970): body : <p align='justify'>The so-called Fiscal Compact is unlikely to be approved by the Latvian parliament in the second and final reading as four opposition MPs said that they would not back the initiative, local media reported on Tuesday evening. The second reading is scheduled for May 31. Under local legislation, the bill should be backed by 67 MPs in the 100-seat parliament, though the ruling coalition controls only 56 seats and depends on the opposition Union of Greens and Farmers (ZZS) to approve it. During the first reading 68 MPs backed the bill. The four MPs from ZZS will meet PM Dombrovskis later today to discuss the issue. They want more clarity about the country’s participation in neighboring Lithuania’s nuclear power plant project. Previously, ZZS said it extends its support on condition that the government fights for higher subsidies for farmers in the 2014-2020 EU budget framework.</p>
Here is my code :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + bodyTxt + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
and the result is this :
I've tried using body.loadData(newBody, "text/html", "UTF-8");
, but this way didn't even show me the result.
So any ideas which I'm doing wrong and how can I fix this?
Thanks in advance!
As I can see from your logs you are receiving the html tags not with < symbol,but like >. I guess if you replace these characters in your html string it will work as it should be. Try this :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
String replaced = bodyTxt.replace("<","<");
Log.d("","replaced : "+replaced);
String replaced2 = replaced.replace(">", ">");
Log.d("","replaced2 : "+replaced2);
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + replaced2 + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
you can use this
webview.loadData(Html.fromHtml(htmlData),"text/html","utf-8");
This is how i do it;
String htmlContent = "<html><body>hello</body></html>";
WebView wv = (WebView) findViewById(R.id.my_view);
wv.loadData(htmlContent, "text/html", "UTF-8");
Investigate how the data looks in the db, all the "<>" and such are quoted with "<", this might be the problem you looking for.
Related
I have a String with (\n) as a line break
String mytext = "<p>Your email is not correct.\nPlease check again</p>"
//(mytext value load from my database)
And i put mytext on webview like this :
web_view.loadDataWithBaseURL("", mytext, "text/html", "UTF-8", "");
But, i don't know why \n doesn't work as line break, but it show as String "\n"?
How to fix it?
I tried to replace \n with </ br> with mytext.replace("\n","</ br>") and mytext.replaceAll("\n", "</ br>") but it doesn't work.
Thanks
Updated
So the solution is :
mytext.replace ("\\r\\n", "<br>").replace ("\\n", "<br>");
Thank you, #ChristianJonassen for your help.
You are using HTML, you need to introduce an HTML line break.
In your case, the source code of the page will be given the line break, but not the representation given to the user.
Here is how you can fix it:
String mytext = "<p>Your email is not correct.<br />Please check again</p>"
Since it comes from an external service, you can edit the String by replacing \n:
mytext.replaceAll("\n", "<br />");
I have some problem in my web view.
I want to load a string from a WordPress blog, witch have html tags such as <a> and image tag and ... .
So my problems are:
As I mention above, I want to load a local string and I want to handle user click on the links, so I load data like this into the web view:
WebView webview = (WebView) this.findViewById(R.id.mainWV);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
String s="<p>It will enable Seattle-based Alaska to expand into lucrative hubs such</p>\n<p><img class=\"aligncenter size-full wp-image-1035\" src=\"http://ichef.bbci.co.uk/news/660/cpsprodpb/D09F/production/_89070435_89069565.jpg\" alt=\"\" width=\"300\" height=\"120\" /></p>\n<p>as San Francisco and Los Angeles.</p>\n";
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
and another way I tried was:
String head1 = "<head></head>";
String text = "<html>" + head1
+ "<body dir=\"rtl\" >" + s
+ "</body></html>";
webview.loadData(text, "text/html", "utf-8");
and my client is :
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("USER_CLICKED", url + "USER_CLICKED");
return true;
}
}
Ok, now when I run the app, and when I click on <a> I never see 'USER_CLICKED', but the webview content change and it seems web view is empty, I mean is white as snow.
notice 1: when I try this:
webview.loadUrl("https://android-arsenal.com/");
and run the app, when I click on the links in the loaded web view, every thing is OK, and i see this Log: 'USER_CLICKED' and the related URL.
notice 2: yes i try a lots of different URL, but loading from string, nothings change in click handling.
notice 3: I test in android 5.1 and 4.1 in 4.1 clicked recognized and is see 'User.. but in the 5.1 the white page story happens.(Edit: android 6 also do not show 'USER... ')
my number 2 problem is, when I call this:
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
the image tag does not load! I mean it is just ignore to load the images, and I do not know why.
notice 3: when I copy text from inside the web view there is some rectangle in the text.
OK every one, after a long time, i find the problem, so as i mentioned above, i take string from WordPress rest API(json), so in my word, the string should be OK, but as i find out, there is some extra '\' in the string, and the string is like:
<p>It will enable Seattle....
, as simple it is i just use:
s=s.replaceAll("\\","");
so, thank any one who see this post.
A URL is a URL. The first item is HTML, not a URL. If you want to load the URL then you have to pass a valid URL (not HTML) and do not expect the URL to be magically parsed out of the HTML string that you are loading. In fact the first item is not even valid HTML, it is a part of HTML, potentially a snippet, but it's not even enclosed in the HTML tags that I expect a web view would need.
Hi i have web application which uses mailto like
Send email
but no meter what i do it alway replace space with + sign
Try to decode your String
just use Uri.encode(String).
subject = (EditText) findViewById(R.id.subject);
subject.setText(Uri.encode(WhatEverYourStringVarIs));
This will help you to decode the URIencode.
If you mean the PHP (HTML) Issue just do the same:
$encodeString = urlencode($encodeString);
echo $encodedString;
So the String will displayed proper.
See this url encode.
Okay, but when i'm read your Question the third time: You can use Spaces in these HTML case. It is an Android Bug. Just use this:
Send Mail
Actually i am retrieving text from json web service which is HTML text containing tags and all that ,which i am executing with Html.fromHtml ,but the problem is that the Special Characters are displayed as diamond with question mark which i am not able to display in its correct form.
below is the code where i got the problem
Description=Description.replaceAll("\\<.*?>","");
Description=Description.replaceAll("\\“", "");
Description=Description.replaceAll("\\”", "");
// Description=Description.replaceAll("♦", "");
//Description=Description.replace("“", "");
details.setText(Html.fromHtml(Description).toString());
Description is the String variable where i have stored the html text,i have googled alot but doesn't get anything.hope i got something from here
thanks in advance
In my webview I did sothing like this.
productInfoWebview.loadData(value, "text/html; charset=UTF-8", null);
Decoding HTML is decoding HTML entities to Java raw unicode characters.
String html = "B & This is HTML";
String java = Html.fromHtml(html);
> Output: "B \u0026 This is HTML"
String strJava = Html.fromHtml(html).toString();
> Output: "B & This is HTML"
I have a problem with an URL that starts with https. It's showing a blank white screen whereas links, starting with http, loads successfully in Android webview.
With logs I'm sure that data is coming from HTTPS also but is not displaying in the view. Please help me. Some sample code or link will be most appreciated.
HI,
I read your question, I don't know what is your exact issue if you have data then you can use these line of code to display it in webview
encoding contains html tags like <p> aadfasdf asdf ds </p>
public static final String HTML_FORMAT =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" +
"<html><head><title></title>"+
"<style type=\"text/css\">" +
"'body {color:#FFFFFF;background-color:#000000;font-size: 10pt}'" +
"</style>" +
"</head>" +
"<body>" +
"%s" +
"</body></html>";
String textHtml = String.format(Constants.HTML_FORMAT,encoding);
wv.setBackgroundColor(Color.BLACK);
wv.loadDataWithBaseURL(null, textHtml, "text/html","utf-8", null);
try this hopefully you will get your required result.