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"
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 want to implement a Rich text editor by using a webview in android. Content can be loaded by using a HTML file (which resides in assets) without any problem. But if user has edited the content of the webview (with all the formatting), I need to get the modified content as a HTML string and save it in the database. How can I do this?
I tried in many ways but it seems that we need to pass a URL to get the content of the webview. But after editing the webview content, how can we get the edited URL? or current updated webview content to HTML formatted string?
Using below code I made editable web view.
String msgBody = "<html>\n"+
"<body>\n"+
"<div id=\"content\" contenteditable=\"true\" style=\"font-family:Helvetica;font-size:14px\">" + a +" </div>\n"+
"</body>"+
"</html>";
// wbview = (WebView)this.findViewById(R.id.wbview);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.loadDataWithBaseURL("", msgBody, "text/html", "UTF-8", "");
wbView.setHorizontalScrollBarEnabled(true);
wbView.setVerticalScrollBarEnabled(true);
In iOS we can get it easily by using below code line.
NSString* html=[_tbEmail.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].innerHTML"];
I need to plug an HTML formatted string into a TextView. I'm retrieving the text from a database and when I insert it using this code
WebView webview = (WebView) view.findViewById(R.id.webView);
text = "<html><body>" + {text retrieved form database} + "</body></html>"
webview.loadData(text, "text/html", "utf-8");
It works but this code
TextView output = (TextView) view.findViewById(R.id.simple_text);
output.setText(Html.fromHtml(text));
strips out all HTML formatting.
What is the right way to do this?
Start by getting rid of the <html><body> and </body></html> parts, as Html.fromHtml() does not use them.
Then, limit your HTML to use tags that are supported by Html.fromHtml().
I am going to get the text in an EditText and then display the text in a WebView. The following code works for ASCII characters. For non-ASCII characters, the text in WebView becomes garbage characters.
String input = mEditText.getText().toString();
String html = makeHTML(input); // append HTML elements and headers including MIME and ENCODING header
mWebView.loadData(html, "text/html", "utf-8");
I thought that I was doing something wrong with my HTML, so I try to display the text directly in the WebView without modify the text. However, the result was the same.
String input = mEditText.getText().toString();
mWebView.loadData(input, "text/html", "utf-8");
The makeText() of Toast which displays non-ASCII text in EditText without any problem.
Does anyone know the answer?
WebView might not be able to load certain "unsafe" HTML characters. Try using:
String input = mEditText.getText().toString();
String html = makeHTML(input);
String encodedHtml = URLEncoder.encode(html,"UTF-8");
mWebView.loadData(encodedHtml, "text/html", "utf-8");
The URLEncoder.decode(encodedHtml,"UTF-8") method might also be useful.
Finally, I solve the problem by using loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl) of WebView
mWebView.loadData(data, mimeType, encoding)
mWebView.loadDataWithBaseURL("", data, mimeType, encoding, "")
seems to be same but actually does not.
In my case, loadData() failed to encode the characters properly and failed to load images saved in the asset folder.
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.