As I am getting the html text from the service and I need to display the text on text View.
Managing Safely <b> End Of Course Theory Test </b> <span style="color:red;"> part1 </span>
I am setting this text like
tv.settext(Html.fromHTML("Managing Safely <b> End Of Course Theory Test </b> <span style="color:red;"> part1 </span>"));
It is showing bold but not showing the color red text.
As #ρяσѕρєя K told above that HTML span tag is not supported by Html.fromHtml.
You should either change the service
OR
add a following line to your code, it will change the span color html to font tags, atleast in your case.
String yourHtmlText = yourHtmlText.replace("span style=\"color:", "font color='").replace(";\"","'").replace("</span>", "</font>");
For others I'll recommend to use String.split frequently according to your needs and it will work like magic.
I hope this works.
Cheers :)
As you can see HTML Tags Supported By TextView HTML span tag is not supported by Html.fromHtml.
So you should return only supported tags from server like font,div,p,... or use webview to show all html tags
you have to use following code to show data from html
tv.settext(Html.fromHTML("Managing Safely <b> End Of Course Theory Test </b> <font color='red'>simple</font>"));
enter text seperately
TextView text = ... // find or instantinate your text view.
text.setText(Html.fromHtml("<font color='#ff0000'>text</font>"));
or use spannable string
text.setText("");
text.append("Add all your funky text in here");
Spannable sText = (Spannable) text.getText();
sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);
use this library support all html tags.
https://github.com/NightWhistler/HtmlSpanner
Related
I try to use html-tags for my text in EditText and I want to save it in Room database. So I created a string with html tags like that:
EditText.setText("Hi <font color="#4B0082">there</font>".parseAsHtml())
It works, but when I try to get it back from the EditText like that:
EditText.text.toHtml()
I get this:
<p dir="ltr">Hi <span style="color:#4B0082;">there</span></p>
Why EditText changes my tags and adds something else? I don't need "p" and "span" tags at all. What am I supposed to do? How to get my original string back from the EditText?
Android views, in general, do not operate directly on HTML tags. When you set the text to the EditText, a translation occurs to Spans. In your example,
"Hi <font color="#4B0082">there</font>"
the font color is translated to a ForegroundColorSpan.
When you try to get the text back from the EditText, a reverse translation occurs and the ForegroundColorSpan is translated to an HTML span tag with a style that specifies the font color. This is equivalent in appearance to the original HTML code.
I assume, since your are placing the text into an EditText, that the text could be changed and what you want is the updated text encoded into the same type of HTML that you placed into the EditText. (If you simply want to retrieve exactly the same string back that you put into the EditText you could try using a View tag).
I think that the only way to get the translation you want is to write your own conversion routine. You can get the spans from the text with getSpans(). You would write something similar to Html.toHtml(). This could be a simple or hard task depending upon how robust the translation needs to be.
There could also be a library that can be customized, but I am unaware of one.
Try this one:
String text = Html.toHtml(yourEditText.getText())
It should return you a text with html formatting
Hy! I am working on my Android app and I want to format text so that my TextView shows this text on CONTACT US to look EXACTLY like on that website, but I dont know how to format it in strings.xml file. I have tried with basic HTML formatting putting <br> tag for new row and <a> tag for email adresses and web site, but nothing change. How to format it ? Can so large strings be saved into strings.xml ?
http://visitsplit.com/#pages?page=409
You can use \n for new lines.
Further, you can use Html.fromHtml to create a Spanned that would have basic formatting - bold, italic, links, etc.
You would then need to override the movement method to actually make links operational:
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);
You can use HTML tags, like <br> and <a>, by using:
yourTextView.setText(Html.fromHtml(getString(R.string.your_string)));
Make sure you properly escape any special characters in your strings.xml.
How do I implement something like this:
I want to do that within a single TextView. Setting the background color of the TextView will not do the job.
I guess it should be possible with a spannable, but I don't know how.
In html I would do that:
<html>
<body>
<span style="background:#CCCCCC;">first line <br /> second long line </span>
</body>
</html>
But i don't know how to do that on Android.
The very last option would be to split the text into two text view, and set each textviews background color and with/height to wrap_content. But since the text length / count of words can be dynamically, I need to calculate the length of each word etc. There must be a better way :)
something like this should do the job
String myString = "first line\nsecond long line";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(0xFFCCCCCC),0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spanna);
if you like to keep it in html you can use a WebView and load your html there, styling should be easier with css, I think
I want to display the HTML
"<html><body><font color=\"black\">
In the name of <font color=\"blue\"><b> God</b></font>
the compassionate, the merciful</font></body><html>"
in a TextView. So I have used Html.fromHtml(), but output is different compared with browsers.
The difference is that the word "God" does not appear in blue color in the TextView.
Code:
textView.setText("<html><body><font color=\"black\">
In the name of <font color=\"blue\"><b> God</b></font>
the compassionate, the merciful</font></body><html>");
Html.fromHtml(String) does not support all HTML tags
List of allowed HTML tags:
br
p
div
em
b
strong
i
dfn
big
small
font
blockquote
tt
a
sup
sub
Try out like this:
textView.setText(Html.fromHtml("<html><body>In the name of<font color=blue><b> God </b></font>the compassionate, the merciful</body><html>"));
Note: Black is default color so you dont have to use <font> tag for black color.
if you want to use then complete tags right way like (for example for GRAY and BLUE):
textView.setText(Html.fromHtml("<html><body><font color=gray>In the name of </font><font color=blue><b> God </b></font><font color=gray>the compassionate, the merciful</font></body><html>"));
Looks like this issue was fixed in HtmlCompat. So try using this instead of old deprecated Html:
import androidx.core.text.HtmlCompat;
....
HtmlCompat.fromHtml(stringVar, HtmlCompat.FROM_HTML_MODE_LEGACY);
Does any one know how to make an EditText control shows html formatted text, and even writes HTML formatted text, to act like an HTML Edit Box,
To show HTML code in an EditText you need to convert it to Spanned using Html.fromHtml(). Only a small HTML tag subset can be used in this method. You can set a Spanned as a text of an EditText. Then you can edit it and convert it back using Html.toHtml().
Try using Html.fromHtml(text) to display HTML formatted text in EditText.
But Html class can recognize only few tags, its not comprehensive. You can check this blog to check which all tags are supported as of now.
String xyz="this <br>is<br> my<br> testing <br>string ";
String formattedString =Html.fromHtml(xyz);