I have got the following issue. I have a String which I want to display in a TextView. Because the String can be HTML formatted I am using a Spanned like so:
TextView tv = (TextView)findViewById(R.id.Description);
Spanned myDescription = Html.fromHtml(myDescriptionOriginal, null, null);
tv.setText(myDescription, TextView.BufferType.SPANNABLE);
The "problem" I now have is that when the myDescriptionOriginal is not HTML formatted, the fromHtml(...) removes all newlines (and presumably also things like tabs).
Is there a general solution (perhaps as part of the Spanned/Spannable/Html classes, with which I am not too familiar) that solves this problem or is this something I have to write myself (I could for instance check if the myDescriptionOriginal contains HTML tags and if it doesn't I don't use the fromHtml)
Related
A number of discussions on here going back years related to getting hyperlinks to work in a TextView. The conclusion is that autoLink works for parsing out URLs that are simply embedded in the text, e.g., "go to www.google.com". Then there is
setMovememtMethod(LinkMovementMethod.getInstance());
that will cause actual HTML tags to work, e.g. go to Google. However, using the latter causes autoLink to not work in the same view.
My issue is that I am displaying text that is supplied by a user database, so I have no control over the formatting. In some cases, this text has plaintext links while in others it is entered as HTML tags. Is there any way to get both types of links to work at the same time?
Both plain text links & links with HTML tags will work with the below code
TexView in xml
<TextView
android:id="#+id/txt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textColorLink="#06b" />
Here in activity.java
String text = "this is the link with anchor tag Google. here is the plain text link http://www.google.com";
TextView textView = (TextView) findViewById(R.id.txt_view);
textView.setText(getParsedLinks(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
instead of using android:autoLink="all" in xml or Linkify.addLinks(textView, Linkify.ALL) use Linkify.addLinks to SpannableString as in below method
SpannableString getParsedLinks(String txt){
Spanned span = Html.fromHtml(txt);
URLSpan[] urlSpans = span.getSpans(0, span.length(), URLSpan.class);
SpannableString s = new SpannableString(span);
Linkify.addLinks(s, Linkify.ALL);
for (URLSpan urlSpan : urlSpans) {
s.setSpan(urlSpan, span.getSpanStart(urlSpan), span.getSpanEnd(urlSpan), 0);
}
return s;
}
How can i set a particular string as a title in Edittext. I want to output to look like something like this:
Title
Some other text.
I have tried to make the text bold and give it a larger style. Something like this:
s.setSpan(new StyleSpan(Typeface.BOLD), 0, length ,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new RelativeSizeSpan(1.2f), 0, length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
That does the trick but i wish if i could instead create a h1 tag around the string so the formatted html code i extract from Edittext can be displayed in textview or used elsewhere.
Update
It seems like a problem with Html class. Try something like this.
Log.d("Test", Html.toHtml(Html.fromHtml("<h1>Hello</h1>")));
You would get something like this in your logs:
<p dir="ltr"><b>Hello</b></p>
It seems like Html class is capable of converting h1 tags to spannable but not capable of being able to convert it back to h1 tags.
The fromHtml method converts the header tags to Spannable using a similar trick i already stated above. It's not implemented in the toHtml method and hence it don't create header tags from Spannable. Can someone try to fix this.
You can take a look at HTML.fromHtml(String text)
This will parse HTML and return a spanned string. The only thing notable is, that it's a bit tricky modifying the styles used for the spans.
Try something like this. Which worked for me
SpannableString text = new SpannableString(Html.fromHtml(<h1>Your content</h1>));
I have a string that contain html tags. So for set text into TextView I use:
textView.setText(Html.fromHtml(myString));
It's works good.
Also I have own method convertText() with type SpannableStringBuilder. In which method I convert text, where I have something like [text="information"]. I use next for set text:
textView.setText(convertText(myString), TextView.BufferType.SPANNABLE);
For example: myString = "<h1> This is [text="information"]. </h1>"
The result must be:
This is INFORMATION. (with h1 formatted text. caps lock of word information works in method convretText() good)
It's also works good. But how I can use these two setText approach in one time?
I am trying to get a textview to process a hyperlink as well as phone numbers. Say my text is:
"555-555-555, www.google.com, Google!"
If I run Html.fromHtml() on this string, then the TextView shows Google! correctly as a clickable link but not the other two.
If I run Linkify.addLinks(TextView, Linkify.All) on the TextView, then the first two are correctly recognized as a phone number and url, but the html is not processed in the last one.
If I run both of them, then either one or the other is honored, but not both at the same time. (Html.fromHtml will remove the html tags there, but it won't be a link if linkify is called after)
Any ideas on how to get both of these functions to work simultaneously? So all the links are processed correctly? Thanks!
Edit: Also, the text is changed dynamically so I'm not sure how I would be able to go about setting up a Linkify pattern for that.
It's because Html.fromHtml and Linkify.addLinks removes previous spans before processing the text.
Use this code to get it work:
public static Spannable linkifyHtml(String html, int linkifyMask) {
Spanned text = Html.fromHtml(html);
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, linkifyMask);
for (URLSpan span : currentSpans) {
int end = text.getSpanEnd(span);
int start = text.getSpanStart(span);
buffer.setSpan(span, start, end, 0);
}
return buffer;
}
try to set movement method on your textview instead of using Linkify:
textView.setMovementMethod(LinkMovementMethod.getInstance());
In your TextView's xml layout, you should add the following:
android:autoLink="all"
android:linksClickable="true"
Then you should remove your Linkify code in Java.
It works somehow, but I dont know why. I added a question to see if someone can explain the behavior: Using Linkify.addLinks combine with Html.fromHtml
I have a block of text coming from a webservice, and depending on some tags which I have predefined, I want to style the text before setting it to my TextView. For bold, italics, and underline, I was able to do this easily with the replaceAll command:
PageText = PageText.replaceAll("\\*([a-zA-Z0-9]+)\\*", "<b>$1</b>");
PageText = PageText.replaceAll("=([a-zA-Z0-9]+)=", "<i>$1</i>");
PageText = PageText.replaceAll("_([a-zA-Z0-9]+)_", "<u>$1</u>");
txtPage.setText(Html.fromHtml(PageText), TextView.BufferType.SPANNABLE);
So, to bold a word, surround it with *'s, for italics, surround with _.
But, for strikethrough, Html.fromHtml does not support the "strike" tag, so it can't be done this same way. I've seen examples of using Spannable to set the styling on one section of text, but it requires positional numbers. So, I guess I could loop through the text, searching for - (the tag to represent the strike), then searching for the next one, spanning the text in between, and repeating for all such strings. It will end up being 10 lines of looping code as opposed to 1 for the others, so I'm wondering if there is a more elegant solution out there.
If it is just TextView you can strike through using paint flags
TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
#Suresh solution works if you want to strikethrough the entire TextView but if you want to strikethrough only some portions of the text then use the code below.
tvMRP.setText(text, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) tvMRP.getText();
spannable.setSpan(new StrikethroughSpan(), 3, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Here text is the text which we want out TextView to display, 3 is the no. of characters (starting from 0) from where the strikethrough will start.
You can do it with a custom TagHandler such as the one on this SO question:
Spanned parsed = Html.fromHtml(PageText, null, new MyHtmlTagHandler());
And the TagHandler implements the methods:
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
processStrike(opening, output);
}
}
....
Are you sure Html.fromHtml doesn't support <strike>? It's listed in this Commonsware blog post
It looks like is not really supported, at least it does not work on Android 3.1.
#RMS2 if text is small you can split it into two or three separate text views and apply flag only to the one which you want, not perfect for long texts ;(
Most of the applications we work in are going to use text somewhere throughout the project and thankfully, KTX provides some extension functions when it comes to these parts. For text, we essentially have some functions available for the SpannableStringBuilder class.
For example, after instantiating a Builder instance we can use the build methods to append some bold text:
textView.text =buildSpannedString {
strikeThrough {
append(
value ?: ""
)
}
}