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().
Related
How to Justify paragraph in textview setText option? I am loading data from API and need to display paragraph as justified.
textv.setText(Html.fromHtml(text));
I have tried below in adapter but nothing works
text = text + ""+paragrph +"";
Is it possible other than webview ?
You cannot set the property like gravity in TextView. You can either use WebView or other open source libraries.
Android does not support text justification yet.
You need to use WebView to justify text. Here is it an example:
WebView webView = (WebView) findViewById(R.id.webView);
webView .setBackgroundColor(Color.TRANSPARENT);
String header = "<html><head><style type=\"text/css\">#font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/Walkway-Ultrabold.ttf\")}body {font-family: MyFont;font-size: medium;text-align: justify;}</style></head><body>";
String footer = "</body></html>";
String text_web_view = header + message + footer;
webView.loadDataWithBaseURL(null, text_web_view, "text/html", "UTF-8", null);
This code set also a font placed into assets/fonts folder.
I am trying to understand how I can get my WebView to "Linkify" the email addresses. I want the email address to generate a hyperlink with mailto tag. All the browsers seems to be able to do this automatically. I am trying the following but I still do no get the links to show in the WebView UI.
HtmlString = "<html><head></head><body><label>Email:</label><span>xxx.yyy#example.com</span></body></html>"
SpannableString sp = new SpannableString(HtmlString);
Linkify.AddLinks(sp, MatchOptions.EmailAddresses);
string linkifiedContent = sp.ToString();
WebView.LoadDataWithBaseURL(BaseURL, linkifiedContent, MimeType, null, null);
Linkify is mostly used with TextView. It is used to create clickable links inside TextView.
In your case, you are already making use of WebView.
Hence, you can just add the email address inside <a> tags:
var HtmlString = "<html><head></head><body><label>Email:</label><span><a href='mailto:xxx.yyy#example.com'>xxx.yyy#example.com<a/></span></body></html>";
webView.LoadData(HtmlString, "text/html", "UTF-8");
This should load your Html content perfectly.
Happy coding!
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"];
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 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.