How to process text image for HTML and Linkify

I am trying to get a textual representation for handling hyperlinks as well as phone numbers. Say my text is:

"555-555-555, www.google.com, <a href="www.google.com">Google!</a>" 

If I run Html.fromHtml () on this line, then TextView will display Google! correct as a click link, but not the other two.

If I run Linktext.addLinks (TextView, Linkify.All) in TextView, then the first two will be correctly recognized as a phone number and URL, but html is not processed in the latter.

If I run both of them, then one or the other is respected, but not both at the same time. (Html.fromHtml will remove the html tags there, but it will not be a link if linkify is called after)

Any ideas on how to make both of these functions work at the same time? So, are all the links handled correctly? Thanks!

Edit: Also, the text is changed dynamically, so I'm not sure how I can configure the Linkify template for this.

+6
source share
3 answers

This is because Html.fromHtml and Linkify.addLinks deletes the previous intervals before processing the text.

Use this code to make 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; } 
+4
source

try setting the move method in text mode instead of using Linkify:

 textView.setMovementMethod(LinkMovementMethod.getInstance()); 
+1
source

In the TextView xml layout, you should add the following:

 android:autoLink="all" android:linksClickable="true" 

Then you must remove your Linkify code in Java.

It works somehow, but I don’t know why. I added a question, can anyone explain the behavior: Using Linkify.addLinks in conjunction with Html.fromHtml

0
source

All Articles