7

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"/>
Anil
  • 1,605
  • 1
  • 14
  • 24
Gurjeet Singh
  • 597
  • 5
  • 13

4 Answers4

3

This worked for me: I added autolink in the xml or you can also use linkify in code.

 <TextView
    android:id="@+id/mail"
    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:background="@color/mlb"
    android:autoLink="email"
    android:text="@string/f2"
    />

In the java file:

TextView mtextView = (TextView) findViewById(R.id.mail);

    Spannable sa = (Spannable)mtextView.getText();
    for (URLSpan u: sa.getSpans(0, sa.length(), URLSpan.class)) {
        sa.setSpan(new UnderlineSpan() {
            public void updateDrawState(TextPaint tp) {
                tp.setUnderlineText(false);
            }
        }, sa.getSpanStart(u), sa.getSpanEnd(u), 0);
    }ere
Gurjeet Singh
  • 597
  • 5
  • 13
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(sampleHtmlString, HtmlCompat.FROM_HTML_MODE_LEGACY) as Spannable

sampleSpannable.removeAllUrlSpanUnderline()

Now you're free to set sampleSpannable to your TextView

James
  • 4,573
  • 29
  • 32
  • You cant set sampleSpannableWithoutUnderlines to yout TextView because the function does not return anything. Instead you just set the sampleSpannable and it works! – Dantalian Jun 24 '22 at 14:03
  • @Dantalian you're right, not sure what i was doing there. Anyway, I updated the answer – James Jun 24 '22 at 18:49
0

Here is how

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

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

Remove underline from links in TextView - Android

Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
-2

You can simply add clickable to textView

<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:clickable="true"      
   android:background="@color/mlb"
   android:text="@string/f2"/>
Ahsan Saeed
  • 701
  • 10
  • 22