I tried to do this:

  1. I create the Elements class from Jsoup and make text.select("a")
  2. Then I do foreach for each link and in the text (Before this, through text.html() ) I do

     text.replaceFirst("/<a\\\\b[^>]*>(.*?)<\\\\/a>/Um", "\tССЫЛКА!!!!!\t"); 

And upon returning the formatted text I add it to the TextView and after that I do

 mTextView.setMovementMethod(LinkMovementMethod.getInstance()); 

The first problem is that the regular expression is not replaced with another string. Maybe there is a less sophisticated way?

  • On the Internet there are many different sites where you can play regulars. For example, here is java-regex-tester.appspot.com - rjhdby
  • @rjhdby Yes, the fact is that the site highlights the necessary areas, but the application does not - Herrgott 1:06 pm

2 answers 2

Use Html.fromHtml and do not suffer with regulars, they are not needed here:

 textView.setText(Html.fromHtml(htmlText)); 

Unfortunately, if your links are relative, they will not work when clicked, but this can be corrected using the method (taken from here ):

 public Spanned correctLinkPaths(Spanned spantext) { Object[] spans = spantext.getSpans(0, spantext.length(), Object.class); for (Object span : spans) { int start = spantext.getSpanStart(span); int end = spantext.getSpanEnd(span); int flags = spantext.getSpanFlags(span); if (span instanceof URLSpan) { URLSpan urlSpan = (URLSpan) span; if (!urlSpan.getURL().startsWith("http")) { if (urlSpan.getURL().startsWith("/")) { urlSpan = new URLSpan("http://domain+path" + urlSpan.getURL()); } else { urlSpan = new URLSpan("http://domain+path/" + urlSpan.getURL()); } } ((Spannable) spantext).removeSpan(span); ((Spannable) spantext).setSpan(urlSpan, start, end, flags); } } return spantext; } 

http://domain+path replace with your primary domain, after which the initial code turns into:

 textView.setText(correctLinkPaths(Html.fromHtml(htmlText))); 
  • fromHtml() returns android.text.Spanned . Added toString() returns as text without references. Generally ignores html tags. Added mTextView.setMovementMethod(LinkMovementMethod.getInstance()); after setText(htmlTextFormatted) - does not work - Herrgott
  • one
    Why did you add toString() ? You are Spanned and need to shove in setText . This is a formatted text, which also can highlight links. And toString() removes all formatting. - xkor
  • Works. Only for some reason he clicks on the link, writes that not a single suitable application has been found, although the links are normal, for the browser - Herrgott
  • Show an example of your usual links. Fully tag <a>. - xkor
  • <a href="https://25.mvd.ru/news/item/7748412/" target="_blank">сообщает</a> <a href="/accidents/2016/05/11/147225/" target="_blank">Напомним</a> Along the way, target is to blame - Herrgott
 changedText = text.replaceFirst("<a[^>]*>[^<]*</a>", "\tССЫЛКА!!!!!\t")