Skip to content

Instantly share code, notes, and snippets.

@benvium
Created August 29, 2014 10:49
Show Gist options
  • Save benvium/e2d1897d00d7850fb6dc to your computer and use it in GitHub Desktop.
Save benvium/e2d1897d00d7850fb6dc to your computer and use it in GitHub Desktop.
Remove underlines from TextView links. Based on stackoverflow code.
package com.util.util;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.widget.TextView;
public class TextViewUtils {
private static final String TAG = TextViewUtils.class.getSimpleName();
private static class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
// http://stackoverflow.com/questions/4096851/remove-underline-from-links-in-textview-android
public static void stripUnderlines(TextView textView) {
Spannable s = (Spannable)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);
}
}
@umair-aamir-synnapps
Copy link

Doesn't work

@lepinay
Copy link

lepinay commented Jul 15, 2015

works fine with me

@Dabami
Copy link

Dabami commented Sep 17, 2015

It works perfectly.

@grumpyshoe
Copy link

For me it doesn't work for phone numbers!

Example Text:
Test Test Test Test 0542 156985

Number is still underlined because replacement doesn't work... :(

@divyasavaliya
Copy link

does'nt work phone num

Copy link

ghost commented Jun 24, 2017

Works , Thanks

@GabrielTorelo
Copy link

Works perfectly. Thanks

@benvium
Copy link
Author

benvium commented Jan 25, 2021

@GabrielTorelo

Glad it's still useful after 7 years!

@avii-7
Copy link

avii-7 commented Mar 13, 2024

Thanks it works for .Net-Android <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment