109

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

email.setText(c.getString(5));
    website.setText(c.getString(6));
    Linkify.addLinks(email, Linkify.ALL);
    Linkify.addLinks(website, Linkify.ALL);

Can I do that from XML or Code ?

Moslem Ben Dhaou
  • 6,897
  • 8
  • 62
  • 93
Mohamed Ben Dhaou
  • 1,124
  • 2
  • 10
  • 16

13 Answers13

243

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);
            s.removeSpan(span);
            span = new URLSpanNoUnderline(span.getURL());
            s.setSpan(span, start, end, 0);
        }
        textView.setText(s);
    }

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) {
            super(url);
        }
        @Override public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    }
Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • 9
    This solution assumes the text of the span is the same as the URL, which is the case for a basic http:// link. However, Linkify is smart and converts a phone number like (212) 555-1212 into the URL `tel:2125551212`. The `new URLSpanNoUnderline` call should be passed `span.getURL()` to retain this info, otherwise you generate bad links that cause exceptions when clicked. I've placed this proposed solution in the edit queue for your answer, since I don't have edit permissions myself. – Mike Mueller Jan 24 '11 at 10:39
  • Cheers Mike. I approved your edit, first time I'd ever *seen* that edit dialog, hope it worked. – Reuben Scratton Jan 24 '11 at 10:48
  • What does "text of the span is the same as the URL" mean? – Shane Oliver Mar 24 '11 at 11:24
  • It means that a word that you read on the screen, which is a link, can be the same or different than the actual link: go there, here the url is "http://myweb.com", while "go there" is the span – Lumis Mar 24 '11 at 14:35
  • But even non telephone numbers are getting displayed as link for example 20.00 can be clickable ..Any workaround for this ? – Nemo Mar 28 '13 at 09:34
  • 8
    Not Working for me. It gives Class Cast Exception at Line Spannable s = (Spannable)textView.getText(); – Brijesh Thakur May 21 '13 at 11:42
  • 5
    Just replace that line with Spannable s = new SpannableString(textView.getText()); – FOliveira Aug 08 '14 at 15:31
  • I've have tried this one, but after calling the function the links stop working. I'm on android 4.3, and i had to replace Spannable s = (Spannable)textView.getText(); with Spannable s = new SpannableString(textView.getText()); – pedrorocha.org Apr 30 '15 at 22:12
  • 1
    replacing with `Spannable s = (Spannable) textView.getText()` with `Spannable s = new SpannableString(textView.getText());` doesn't work for me. If I switch back to casting then it does work, only if the TextView has `android:textIsSelectable=true`. Any idea why? – b.lyte Jun 29 '15 at 17:37
  • Note that this doesn't work when you use textView.setAutoLinkMask(..). – Krishnaraj Sep 30 '15 at 08:25
  • 3
    It works after i removed `android:autoLink="web"` from TextView. Thanks. – Kishore Feb 02 '17 at 12:37
  • @clu textView.getText() only returns a Spannable if the TextView is editable or if it setText() is called with BufferType.SPANNABLE. – Neil Miller Jun 20 '17 at 15:28
  • 1
    @Krishnaraj Same goes for removing `android:autoLink="email"` – finstas Aug 30 '17 at 09:12
  • 3
    Is there any way to make this work with the `android:autoLink` attribute? Otherwise it seems you have to create your own onclick methods for built in functionality. – Alkarin Mar 09 '18 at 00:44
  • This solution doesn't work with `android:autoLink` attribute. Without this attribute it doesn't has such a value. – CoolMind Apr 05 '18 at 10:03
  • Kotlin: ```private fun stripUnderlines(textView: TextView) = SpannableString(textView.text).apply { for (span in getSpans(0, length, URLSpan::class.java)) { removeSpan(span) setSpan(URLSpanNoUnderline(span.url), getSpanStart(span), getSpanEnd(span), 0) } } private class URLSpanNoUnderline(url: String) : URLSpan(url) { override fun updateDrawState(ds: TextPaint) { super.updateDrawState(ds) ds.isUnderlineText = false } }``` – bko Jul 26 '21 at 21:49
  • To make this work with the `android;autoLink` attribute, you have to call `textView.setAutoLinkMask(0)` before calling `textView.setText(s)`, otherwise the autolinking process will run on the new text, which gets rid of our modified spans. – user3210008 Apr 12 '22 at 12:45
38

Given a textView and content:

TextView textView = (TextView) findViewById(R.id.your_text_view_id);
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);
}
textView.setText(s);

This is based on the approach suggested by robUx4.

In order to make the links clickable you also need to call:

textView.setMovementMethod(LinkMovementMethod.getInstance());
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • 8
    this does not work if you try to get a string from resources, so `String content = getResources().getString(R.string.content);` which includes a link no longer works. – Rain Man Aug 03 '15 at 20:01
  • 1
    How can that be? A String does not know where it originated from. There must be a difference in the String contents. – Adriaan Koster Aug 04 '15 at 11:03
  • not sure why but it doesn't show the url when I call the string from `res/values/strings.xml` which contains exactly the same thing as the example. Can you test in your end? – Rain Man Aug 04 '15 at 13:24
  • I'm definitely not going to test at my end (-:. If you want to be sure about the string contents, log both strings together with their hashCode. It would surprise me if you find different results when using the code above with the exact same strings. – Adriaan Koster Aug 05 '15 at 07:50
  • 1
    If you want to make it work with getting string from resources, you need to encode it. `>` = `>`, `<` = `<` etc. For example: `<a href="https://www.google.com/">Google</a>` See https://developer.android.com/guide/topics/resources/string-resource – Micer Aug 10 '18 at 21:17
  • 1
    If you want to make it work with getting string from resources you can also embed the whole HTML string in `<![CDATA[html_string_goes_here]]>`, which looks much cleaner than encoding `<` and `>` – Makks129 Feb 28 '20 at 11:57
25

Here is Kotlin extension function:

fun TextView.removeLinksUnderline() {
    val spannable = SpannableString(text)
    for (u in spannable.getSpans(0, spannable.length, URLSpan::class.java)) {
        spannable.setSpan(object : URLSpan(u.url) {
            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.isUnderlineText = false
            }
        }, spannable.getSpanStart(u), spannable.getSpanEnd(u), 0)
    }
    text = spannable
}

Usage:

txtView.removeLinksUnderline()    
sma6871
  • 3,198
  • 3
  • 38
  • 52
  • 1
    Use: `ds.color = ContextCompat.getColor(this@MainActivity, R.color.myColor)` To change link text color – Andrey Kijonok May 17 '21 at 14:01
  • @Fortran show us your code. which android version you have tested? – sma6871 Jun 14 '21 at 22:06
  • Here's a _slightly_ more Kotlin-y way to do this: ~~~ spannable.getSpans(0, spannable.length).forEach { span -> val startIdx = spannable.getSpanStart(span) val endIdx = spannable.getSpanEnd(span) spannable.removeSpan(span) spannable.setSpan( URLSpanNoUnderline(span.url), startIdx, endIdx, 0 ) } ~~~ – isles1217 Feb 25 '22 at 00:34
11

UnderlineSpan already exists, but can only set the underline.

Another solution is to add a no underline span on each existing URLSpan. Thus the underline state is disabled just before painting. This way you keep your URLSpan (possibly custom) classes and all other styles set elsewhere.

public class NoUnderlineSpan extends UnderlineSpan {
    public NoUnderlineSpan() {}

    public NoUnderlineSpan(Parcel src) {}

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}

Here is how you set it without removing the existing URLSpan object:

URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
    int start = s.getSpanStart(span);
    int end = s.getSpanEnd(span);
    NoUnderlineSpan noUnderline = new NoUnderlineSpan();
    s.setSpan(noUnderline, start, end, 0);
}
miracula
  • 527
  • 7
  • 10
robUx4
  • 853
  • 9
  • 13
5

I've implemented a solution which, in my opinion, is more elegant. I've made a custom TextView. This way you don't need to execute extra code for every TextView with hyperlinks.

package com.example.view;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.URLSpan;
import android.util.AttributeSet;

import com.example.utils.UrlSpanNoUnderline;

public class TextViewNoUnderline extends AppCompatTextView {
    public TextViewNoUnderline(Context context) {
        this(context, null);
    }

    public TextViewNoUnderline(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public TextViewNoUnderline(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setSpannableFactory(Factory.getInstance());
    }

    private static class Factory extends Spannable.Factory {
        private final static Factory sInstance = new Factory();

        public static Factory getInstance() {
            return sInstance;
        }

        @Override
        public Spannable newSpannable(CharSequence source) {
            return new SpannableNoUnderline(source);
        }
    }

    private static class SpannableNoUnderline extends SpannableString {
        public SpannableNoUnderline(CharSequence source) {
            super(source);
        }

        @Override
        public void setSpan(Object what, int start, int end, int flags) {
            if (what instanceof URLSpan) {
                what = new UrlSpanNoUnderline((URLSpan) what);
            }
            super.setSpan(what, start, end, flags);
        }
    }
}

And code for UrlSpanNoUnderline:

package com.jankstudios.smmagazine.utils;

import android.text.TextPaint;
import android.text.style.URLSpan;

public class UrlSpanNoUnderline extends URLSpan {
    public UrlSpanNoUnderline(URLSpan src) {
        super(src.getURL());
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
    }
}
Sam Sunson
  • 53
  • 1
  • 3
3

If you are using Textview autolink property and you want to remove underlines you can use it:

First, extend UnderlineSpan and remove underline:

public class NoUnderlineSpan extends UnderlineSpan {

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}

Second, create and instance of NoUnderlineSpan, create a Spannable from the String text and set the span to the spannable:

NoUnderlineSpan mNoUnderlineSpan = new NoUnderline();
if (yourTextView.getText() instanceof Spannable) {
    Spannable s = (Spannable) yourTextView.getText();
    s.setSpan(mNoUnderlineSpan, 0, s.length(), Spanned.SPAN_MARK_MARK);
}

Reference: http://prog3.com/sbdm/blog/maosidiaoxian/article/details/39156563

Francisco Moya
  • 103
  • 1
  • 8
  • Nice solution, but after return from Telephone app (after click on phone number) it again underlines TextView. – CoolMind Apr 05 '18 at 09:24
  • 1
    Yes, if your activity goes to the background you should use this code in your onResume activity function to maintain the textview no underline state. – Francisco Moya Apr 20 '18 at 15:38
2

Whenever, try to remove URL underline with spannable am i suggest only these things :

1> Creating Custom class :

private class URLSpanNoUnderline extends URLSpan {
            public URLSpanNoUnderline(String url) {
                super(url);
            }
            @Override public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        }

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

2> setSpan with spannable text :

spannableText.setSpan(new URLSpanNoUnderline(UrlText), 0, UrlText.length() , 0);

Here, spannableText is Object of SpannableString...!!!

Dhruv Raval
  • 4,946
  • 3
  • 29
  • 34
2

Here's a Kotlin extension function as a solution to remove all UrlSpan underlines:

private fun Spannable.removeAllUrlSpanUnderline() {
    for (urlSpan in getSpans(0, length, URLSpan::class.java)) {
        setSpan(object : UnderlineSpan() {
            override fun updateDrawState(tp: TextPaint) {
                tp.isUnderlineText = false
            }
        }, getSpanStart(urlSpan), getSpanEnd(urlSpan), 0)
    }
}

In my case, I began with a string with href tags. fromHtml returns a Spanned so cast it to a Spannable so it's mutable. See the sample use below:

val sampleHtmlString = "<a href=\"www.google.com\">first Link</a> and <a href=\"www.google.com\">second link</a>"

val sampleSpannable = HtmlCompat.fromHtml(caslString, HtmlCompat.FROM_HTML_MODE_LEGACY) as Spannable

val sampleSpannableWithoutUnderlines = sampleSpannable.removeAllUrlSpanUnderline()

Now you're free to set sampleSpannableWithoutUnderlines to your TextView

James
  • 4,573
  • 29
  • 32
0
public void setView()
{
TextView t=(TextView) findViewById(R.id.textView3);
        t.setText(Html.fromHtml("<a href=http://www.urdusms.net > UrduSMS "));
        t.setTextColor(Color.BLACK);
        t.setGravity(Gravity.CENTER);
        t.setMovementMethod(LinkMovementMethod.getInstance());

         Spannable s = (Spannable) t.getText();
            URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
            for (URLSpan span: spans) {
                int start = s.getSpanStart(span);
                int end = s.getSpanEnd(span);
                s.removeSpan(span);
                span = new URLSpanline_none(span.getURL());
                s.setSpan(span, start, end, 0);
            }
        t.setText(s);
 }
//inner class is    
private class URLSpanline_none extends URLSpan {
            public URLSpanline_none(String url) {
                super(url);
            }
            @Override public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        }
sandhu
  • 305
  • 4
  • 4
0

If you just want the text and don't bother about the URL link

This will STRIP the link but KEEP the text

private Spannable stripLinks(String content) {
    Spannable s = new SpannableString(content);
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        s.removeSpan(span);
    }

    return s;
}

no additional classes required

String content = "<a href='http://stackoverflow.com'>Stack Overflow</a> Rocks!";
textView.setText(stripLinks(content));
Samuel
  • 9,883
  • 5
  • 45
  • 57
0

Here is My method

 public static void removeUnderlines(Spannable p_Text) {
            if (p_Text != null && p_Text.toString().length() > 0) {
                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);
                }
            }
        }

Call It like this

AppController.removeUnderlines((Spannable) eventEmail.getText());

Appcontroller is my application class where i put this method so that i can access it from anywhere

Bharat singh
  • 504
  • 7
  • 17
0

For Xamarin users finding this post, here is how I got it to work:

  1. Create a custom span class to handle stripping out the underlines.
    class URLSpanNoUnderline : URLSpan {
            public URLSpanNoUnderline (string url) : base (url) {
            }

            public override void UpdateDrawState (TextPaint ds) {
                base.UpdateDrawState (ds);
                ds.UnderlineText = false;
            }
        }
  1. Create an extension method to find all url spans and replace them with our custom spans.
public static void StripUnderlinesFromLinks (this TextView textView) {
                var spannable = new SpannableStringBuilder (textView.TextFormatted);
                var spans = spannable.GetSpans (0, spannable.Length (), Java.Lang.Class.FromType (typeof (URLSpan)));
                foreach (URLSpan span in spans) {
                    var start = spannable.GetSpanStart (span);
                    var end = spannable.GetSpanEnd (span);
                    spannable.RemoveSpan(span);
                    var newSpan = new URLSpanNoUnderline (span.URL);
                    spannable.SetSpan(newSpan, start, end, 0);
                }
                textView.TextFormatted = spannable;
            }
NamNH
  • 1,752
  • 1
  • 15
  • 37
Justin
  • 17,670
  • 38
  • 132
  • 201
0
abstract class NoUnderlineClickableSpan : ClickableSpan() {        
    override fun updateDrawState(ds: TextPaint) {
        super.updateDrawState(ds)
        ds.isUnderlineText = false
    }    
}
JON
  • 965
  • 2
  • 10
  • 28