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
Related
I'm getting a JSON response string similar to this:
<strong>B.<\/strong> Because there is no indication of Miss Manette’s feelings
The string text that I'm receiving is full of tags like <strong>, <em> and ’
“
” etc. How can I parse it to a plain String with same features?
The only way I could think of is replacing such characters and using Html.fromHtml() method. Is there a built-in parser available? How could I parse such HTML text?
Use Html.fromHtml only. It'll parse most of the tags supplied and give you the formatted output. The point to note here is that not all of the HTML tags are supported by this method. Checkout this link for more information about what tags are supported. Also check this, though it's a bit old.
If you know what text you'll be parsing, and you have tags that aren't parsed by fromHtml, your best bet would be to replace them with empty string and then use this method.
Android Html.fromHtml function auto converts undesired text into hyperlinks.
Here is my code:
String htmlContent= "corners of nail.It has to";
textViewContent.setTextHtml.fromHtml(htmlContent));
textViewContent.setText(comment.getContent());
In above code Html.fromHtml treats "nails.It" as a link and converts it into hyperlink.
Here is the result of converted string.
One of the solution which I could think is, put space after fullstop.
Is there any good solution?
You can try something like this.
textViewContent.setText( Html.fromHtml("YOUR DESIRED TEXT"));
textViewContent. setMovementMethod(LinkMovementMethod.getInstance());
Hope it helps.
Provide space between dot and second statement
String htmlContent= "corners of nail. It has to";
textViewContent.setTextHtml.fromHtml(htmlContent));
textViewContent.setText(comment.getContent());
I had found an answer that we can use bullets in textview by adding u\2022 with the text.
Ref : https://stackoverflow.com/a/3429643/4919237
When i added this code through XML it displays fine in TextView
and When i try to display bullet through json it doesnt display bullet ,instead it displays the text u\2022 itself.
my receiving Json format is
{"description":"\\u2022first one\\n\\u2022second one edited\\n"}
please help me to reslove my problem.
At First , You can check Html.fromHtml & remove one \ .
Html.formHtml method takes an Html.TagHandler and an Html.ImageGetter
as arguments as well as the text to parse.
YourTextViewObj.setText(Html.fromHtml("Your String"));
FYI
Currently android seems support the following HTML tags to be rendered on TextView.
You can set text using Html.fromHtml like
textview.setText(Html.fromHtml(your description));
Due to HTML usage within a string resource, I can't convert this to string from a charsequence (I will lose the formatting otherwise).
<string name="exponent_key">x<sup><small>y</small></sup>/string>
After using getString() I want to replace the 'y' with 'other stuff' but how do you do that? It seems like a simple question but for some reason I can't find anything about it.
Edit: Now that I think about it, can I convert the charsequence to a string that contains the HTML code, and then convert it back to a charsequence later?
Edit: Forgot to mention that the string gets set to a button title, and then retrieved (where it is then used).
There is. Create a function where, as a parameter, you take a string that needs to be formatted. And in function, you just take it through itterator, and after that Html.fromHtml()
in your string.xml
<string name="exponent_key">x<sup><small>%1$d</small></sup></string>
in your code
textView.setText(getString(R.string.exponent_key,2))
Let's break down your question in multiple steps:
Replacing the y with "other stuff" can be done like this:
String.format("%1$", "otherStuff");
If you use getString(), you can do the same thing like that:
<string name="exponent_key">%1$</string>
---
String string = getString(R.string.exponent_key, "otherStuff");
For more than one element, do this way:
you can do that like this:
<string name="string_name">%1$ : %2$</string>
---
getString(R.string.string_name, new String[]{"hello", "world"});
In XML you cannot nest HTML code, since HTML is another type of XML and the parser messes up and cannot recognize what tags are for Android and what are for HTML. But.. there's a trick. You can do that in this way:
<string name="exponent_key"><![CDATA[x<sup><small>%1$</small>/sup>]]></string>
So, with the string above in your XML, just use and you're fine:
getString(R.string.exponent_key, "otherStuff");
Note: if you need to show the HTML in a TextView, just use Html.fromHtml:
textView.setText(Html.fromHtml(getString(R.string.exponent_key, "otherStuff")));
Consider the effect you want to achieve.
you can do like this.
SpannableString ss = new SpannableString("2");
// set superscript
ss.setSpan(new SuperscriptSpan(),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set font size
ss.setSpan(new AbsoluteSizeSpan(12,true),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(ss);
I would like to remove the tag in my android which is coming from an web response. And I dont want to use Html.fromHtml in my setText(), as I would not like the text to appear in the next line. Can anyone please suggest me an idea?
You can use replaceAll() function
ex:
html.replaceAll("&","");//remove ampersands
You can use trim on the String returned by Html.fromHtml for avoiding newline before and after the text.
Source: How to remove newlines from beginning and end of a string (Java)?