Remove Underline from Android TextView Link
As you may already know, the TextView object has a property named android:autoLink that creates HTML or email links automatically. You also got properties to change link colors such as android:textColorLink and android:textColorHighlight. The only missing option many of us would like to have is a way to remove the underline under the link itself.
Sadly, removing the underline seems to be somewhat "hard" to achieve. So here's my way of doing this, greatly inspired of the code I found on this blog.
First, let's create a TextView:
<TextView android:id="@+id/myTextView"
android:text="Want to search www.google.com?"
android:textColor="#409bbf"
android:textColorLink="#783302"
android:textColorHighlight="#ff0000"
android:autoLink="web"/>
The TextView above should look approximately like this when rendered in Android:
Want to search www.google.com?
Then we need a "util" class we'll name URLSpanNoUnderline:
package com.evilcodingmonkey.android.util;
import android.text.TextPaint;
import android.text.style.URLSpan;
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String p_Url) {
super(p_Url);
}
public void updateDrawState(TextPaint p_DrawState) {
super.updateDrawState(p_DrawState);
p_DrawState.setUnderlineText(false);
}
}
If you already have a StringUtil-like class (I always have one in my projects), add the following function to it. You may add this function anywhere you want anyway...
/**
* Removes URL underlines in a string by replacing URLSpan occurrences by
* URLSpanNoUnderline objects.
*
* @param p_Text A Spannable object. For example, a TextView casted as
* Spannable.
*/
public static void removeUnderlines(Spannable p_Text) {
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);
}
}
The last thing you have to do to replace all occurences of URLSpan by URLSpanNoUnderline in your TextView objects. I do that by modifying the onCreate function of my Activity classes. For example, this is the onCreate function of the "about" screen of my app:
protected void onCreate(Bundle p_SavedInstanceState) {
super.onCreate(p_SavedInstanceState);
setContentView(R.layout.about);
// Remove underlines from HTML links
TextView txtUrlGoogle = (TextView)findViewById(R.id.textUrlGoogle);
// Make sure the TextView was instantiated correctly
if(txtUrlGoogle != null) {
StringUtil.removeUnderlines((Spannable)txtUrlGoogle.getText());
}
}
And we're done! As you can see, this is totally reusable. All TextView properties such as color will continue to work as they are intended to. As I said at the beginning of my post, I did not invented this way of removing underlines, but I think this will help clarify it, and show how it can be easily reused.
August 29th, 2011 - 22:18
Very helpful example – I was banging my head against the wall trying to remove underlines from links made by Html.fromHtml().
Your code didn’t work fully for me though – I had to setText() with the result of removeUnderlines (and change its return value accordingly) on txtUrlGoogle, then the new Spannable was applied.
In fact, because I was working with Html.fromHtml(), I also had to convert Spanned to Spannable using this code:
Spannable p_Text2 = Spannable.Factory.getInstance().newSpannable(p_Text);
August 10th, 2012 - 02:55
thanks for help.
April 22nd, 2013 - 05:05
Very useful and helpful, was trying to figure out since long but finally got this link n yipee, problem solved.
Thanks