remove underline from clickable email in textview

Viewed 4121

I am working on an android app in which I have put a mail in a text in textView and it is clickable. I want to remove the underline from the mail. How to do it?

 <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/mailnlink"
    android:textColor="@color/mltext"
    android:textColorLink="@color/link"
    android:textStyle="italic"
    android:gravity="center"
    android:autoLink="email"
    android:background="@color/mlb"
    android:text="@string/f2"/>
4 Answers

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(sampleHtmlString, HtmlCompat.FROM_HTML_MODE_LEGACY) as Spannable

sampleSpannable.removeAllUrlSpanUnderline()

Now you're free to set sampleSpannable to your TextView

Related