how to create textview link without underscore in android

做~自己de王妃 提交于 2019-12-05 19:26:38

you can try this. such as

String content = "your <a href='http://some.url'>html</a> content";

Here is a concise way to remove underlines from hyperlinks:

Spannable s = (Spannable) Html.fromHtml(content);
for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) {
    s.setSpan(new UnderlineSpan() {
        public void updateDrawState(TextPaint tp) {
            tp.setUnderlineText(false);
        }
    }, s.getSpanStart(u), s.getSpanEnd(u), 0);
}
tv.setText(s);

You can use Spannable and URLSpan here to remove hyperlink underline from your code

First of all make your anchor tag text into Spannable

Spannable spannedText = Spannable.Factory.getInstance().newSpannable(
            Html.fromHtml(webLinkText));

Create new class URLSpanNoUnderline and extend it with URLSpan and override updateDrawState method. in that method you can set setUnderlineText to false

then use this method you can remove your link

public static Spannable removeUnderlines(Spannable p_Text) {  
       URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);  
       for (URLSpan span : spans) {  
            int start = p_Text.getSpanStart(span);  
            int end = p_Text.getSpanEnd(span);  
            p_Text.removeSpan(span);  
            span = new URLSpanNoUnderline(span.getURL());  
            p_Text.setSpan(span, start, end, 0);  
       }  
       return p_Text;  
  }  

For more information you can visit this link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!