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
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
I would like to display some text in a TextView, but I would like to avoid text wrapping in a specific position of the text, to avoid the text to display like this:
This is my very long text
:
Instead, I want my text to display as following:
This is my very long
text :
You may ask: why is there a space before :?
Simply because it's in French, and, unlike English, you're supposed to add an extra space before this character (also available with ? and !).
I tried this:
TextView myTextView = (TextView) findViewById(R.id.my_text);
myTextView.setText(Html.fromHtml("This is my very long <span style=\"white-space: nowrap;\">text :</span>"));
But of course, it doesn't work.
I was also thinking of a specific character that behaves like a space but without wrapping. I don't know if it exists, and if it's a good practice.
I finally found out a solution for the specific white space wrapping problem: just add \u202F instead of your white space in your string, like for example:
<string name="my_string">This is my very long text\u202F:</string>
My point is i only can write/select 1st line of the textview i want to have for example:
My name is:
12345
Is it even possible to write in 2nd line or more lines?
My name is:\n12345
for more lines use more \n\n\n\n\n :-)
You can also do many thing like....Bold italic or showing in list using one Text view.
Write your text in HTML format. i.e
String youttext="<b>My Name is Avi</b><br><br> <i>I m a Android Developer</i><br><br><ul><li>I m a Blogger too</li></ul>"
Textview yourview=(Textview) findViewById(R.id.textview);
yourview.setText(Html.fromHtml(youttext));
You can show the Text in HTML format too. If you want to change the specific color text and style or shading etc.
I want to Justify the text into textView. I does not want to use web view. because i can't increase the text size at run time for different screens. So I want To use the text view.
I want some things like that.
You can set HTML on a TextView.
So...
String htmlString = "<p align=\"justify\">This is some text in a paragraph.</p>";
mTextView.setText(Html.fromHtml(htmlString));
Good Luck. :)
Is it possible to have a black background around each letter (not for the whole word) in a word in a TextView?
The only way I can think of is by using html in your textView.
Put your text in HTML and then do something like this
TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
String desc = "<font color=\"red\"><a href='http://www.mysite.com/'>Visit my site</a></font>";
contactWeb1.setText(Html.fromHtml(desc));
The above code is from the 3rd link
Here are some related questions
Is it possible to have multiple styles inside a TextView?
How to display HTML in TextView?
Android Linkify links textColor ignored, css style overrides possible?