I am receiving a webservice strings in html code, and needed to put them in this html code to put a TextView.
Example:
String str = "<b>Hello</b>";
// my attempt
textView.setText(Html.fromHtml(str));
// this only convert to <b>Hello</b> instead of running the html code.
Well that's an ugly trick but it works:
textView.setText(Html.fromHtml(Html.fromHtml(str).toString()));
Related
I'm using Wordpress API to get posts in my app. I'm trying to add new line while writing a post. However when I ,for example, add post like this :
THIS IS /n a POST
In my app I can see /n in the textView exactly as I put it above. The same thing is happening when I try to use <br> tag.
Setting post in textView:
String content = String.valueOf(HTML.fromHTML(p.excerpt));
vItem.shortContent.setText(content);
Why is that ?
EDIT!!!
It's working after adding :
String content = p.excerpt.replace("\\n","<br>);
vItem.short_content.setText(Html.fromHtml(content));
Thanks!
Supposing p.excerpt is an HTML String, use
vItem.shortContent.setText(Html.fromHtml(p.excerpt));
instead.
You are seeing the new lines because String.valueOf(Object) calls toString() from the param object. Since Html.fromHtml() returns an Spannable, your code is doing a spannable.toString() so its returning the literal text.
you can also put the html formated text in the string resources directory as follows
<string name="helloworld">
<![CDATA[
This is now formatted:<br />
<strong>Hello world</strong> - this should work fine
]]>
</string>
you can then call the string in your activity as follows
txtView = findViewById(R.id.mytext);
String formatedstring = getString(R.string.helloworld);
Spanned txt = Html.fromHtml(formatedstring);
txtView.setText(txt);
For my app i want to add a FAQ page which contains large amount of text. Some of it has to be bold, underlined. I have an faq_xml with a scrollview. How do i add this large amount of text in a well displayed format? I read some online forums saying about using the html format. I couldn't understand. Some help would be appreciatd!
android:maxLines = "any integer"
android:scrollbars = "vertical"
Then use:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
in your main activity.
You can create a string containing HTML code. This String will then be used in a webView that you will make in you faq_xml.
Check this thread..
You can assign your html code to a string and then apply that css file you learned to create in the above mentioned thread.
Then call the webView like
wv.loadDataWithBaseURL("", YourStringHere, mimeType, encoding, "");
For your ease check out this thread..
I know that I can use the default Html.fromHtml(string) but it highlights links by default. Is there a way to prevent that behavior?
P.S.
I'm trying to feed it straight to TextView using .setText() without saving it to String. I want to keep all the formatting except links.
If you only need text you can fetch it with:
String string = Html.fromHtml(string).toString();
Edit:
Since you want to remove only the links, you can use String.replaceAll before parsing the html:
// Remove <a href*>
html = html.replaceAll("<a href.*?>", "");
// Remove </a>
html = html.replaceAll("</a>", "");
textView.setText(Html.fromHtml(html));
The response of a web service is the following :
<span style="color:#DDFFEE;">You have to pay you bil before <b>12 june 2012</b> for your informations.</span></br></br> You have already a large time.
I want to display this in a TextView.
I can't just simply put the response below in a String and applicate
myTextView.setText(Html.fromHtml(response));
It doesn't work because it can't be a String.
How can I do this dynamically please ?
You just need to put the String you get as a response, inside the fromHtml call, if it's not a String, maybe you just need to append .toString() to it, to get a String..
textView.setText(Html.fromHtml("<h2>This is HTML</h2>"));
You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
here : Similar question
I am fetching few html content from my server for which I am using JSON parsing. But this converts my html content to unicode values.
For Eg: <p>Spend minimum $10 (in a single same-day receipt) at any outlet<\/p> is getting converted to,
;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>
Now if I try to set this to my WebView it displays with HTML tags itself. If I try to encode the data using TextUtils.encode it displays the text with unicode values.
Can anyone help me with this.
How should I fetch a HTML content and display it in WebView?
I am not getting your question exactly but, If you want to load HTML in web view in you can use
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
and if you want to convert < and > like notation you can use Jsoup Library
Guys thanks for your help. But I have solved this issue myself. I have elaborated my way of solving the issue.
What I did is,
1)convert the unicode value to Spanned like this,
Spanned ss=Html.fromHtml(;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>");
2)Now convert this Spanned to String like this,
String tempString=ss.toString();
3)And now set this to WebView which solved the problem,
webView.loadData(tempString, "text/html","UTF-8");
Actually this isn't JSON encoder converts data to HTML entities but some other layer, before it passed to JSON encoder.
JSON have nothing to do with HTML tags, usually only quotes encoded by parser (Unicode is supported by most parsers).
You probably need to change the way data is returned by server, to omit encoding of HTML tags braces to HTML entities or decoding entities backin your app.
Update:
To decode HTML entities used in HTML tags (and others too) you may use StringEscapeUtils.unescapeHTML()
To show the HTML page inside the Webview why you require the JSON. create web view inside the XML and write below code Inside the Activity you can see the HTML page.
webView = (WebView)findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.webView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
webView.loadUrl("http://www.google.co.in/");