Mohamed Ben Dhaou Mohamed Ben Dhaou - 2 years ago 902
Android Question

Remove underline from links in TextView - Android

I am using two

to display links from database, I managed to change link colors but I want to remove the underline

Linkify.addLinks(email, Linkify.ALL);
Linkify.addLinks(website, Linkify.ALL);

Can I do that from XML or Code ?

Answer Source

You can do it in code by finding and replacing the URLSpan instances with versions that don't underline. After you call Linkify.addLinks(), call the function stripUnderlines() pasted below on each of your TextViews:

    private void stripUnderlines(TextView textView) {
        Spannable s = new SpannableString(textView.getText());
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span: spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            span = new URLSpanNoUnderline(span.getURL());
            s.setSpan(span, start, end, 0);

This requires a customized version of URLSpan which doesn't enable the TextPaint's "underline" property:

    private class URLSpanNoUnderline extends URLSpan {
        public URLSpanNoUnderline(String url) {
        @Override public void updateDrawState(TextPaint ds) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download