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);
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
In my text view, I am setting the HTML text:
mTextView.setText(Html.fromHtml("THIS USEFUL INTERESTING HTML TEXT IS DISPLAYED IN ANDROID TEXT VIEW"));
The Text is displayed as below:
The long word at the end is breaking with a hyphen. I want the complete word to go into the next line instead. How can I achieve this? I need to use HTML text only. The text can be dynamic and has to scale across various display size and orientations, I cannot put newline char in the text.
I'd suggest you to use attribute android:ellipsize to prevent breaking up a word
Try this...it should work
mTextView.setText(Html.fromHtml("THIS USEFUL" +"<br/>"+ "INTERESTING HTML TEXT IS DISPLAYED IN ANDROID TEXT VIEW"));
How to have HTML New Line Feature in Android Text View? for more clarity
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
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.
I use Html.fromHtml() in my application to display bold and regular characters in the same TextView. But I have 3 different fonts for italic,bold and regular text, and I don't know how to indicate to my text view to use one or the other.
Please give me any reference or hint.
Thanks in Advance.
if I have not misunderstood you can format a single string with different <font> tag.
For instance :
String toShow = <font: italic ...>italic string</font> <font: bold ...>bold string</font>
Html.fromHtml(toShow)
choose the tag html more appropriate for this purpose. here is a list of supporte html tag.
edit: here an example of the html font tag. As alternative I think you can use SpannableString for the same purpose.