Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove underline from links in TextView - Android

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 ?

like image 938
Mohamed Ben Dhaou Avatar asked Nov 04 '10 12:11

Mohamed Ben Dhaou


People also ask

How can I remove underline from text in Android?

There is a really practical and easy way to remove underline for a text. And that is: textview. setPaintFlags(View. INVISIBLE);

How do I remove underline from text on Samsung?

Originally Answered: How do I remove the underline feature while typing in Android keyboard ? Goto settings > Language & input > Android Keyboard settings > auto correction > off.

What is autoLink in Android?

andorid:autoLink => By using this attribute, android can controls whether links(such as urls, emails, phone and address) are automatically found and converted to clickable links.


1 Answers

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);         }     } 
like image 115
Reuben Scratton Avatar answered Sep 28 '22 10:09

Reuben Scratton