Search code examples
androidtextcolortext-decorations

how to create textview link without underscore in android


i came into this wired situation, my code is as follow

LinearLayout ll = new LinearLayout(this);
TextView tv = new TextView(this);
ll.addView(tv);
tv.setText(Html.fromHtml("<a STYLE=\"text-decoration:none;\" href=\"" 
        + StringEscapeUtils.escapeJava(elem.getChildText("newsLink")) + "\">" 
                + StringEscapeUtils.escapeJava(elem.getChildText("Title")) + "</a>"));
tv.setTextColor(Color.BLACK);

but the style="text-decoration:none" and tv.setTextColor(color.black)both not working, the link is still in blue with underscore, any hints on why they're not working? Thanks!


Solution

  • 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);