Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove Underline from hyperlink's text in android ?

Tags:

android

I have Button and its text, I retrive it from string.xml i.e. I have declared a Button text in res/values in strings.xml like

<string name="Google"><a href=""http://www.google.com\">Google</a></string>

I removed its text color from blue to white but how do I remove its underline? in my .java file I am using only setMovementMethod(LinkMovementMethod.getInstance()); to make the link clickabale... I didn't use Linkify, webview or anything like form.Html... everything works fine. I just want to remove the underline below that "Google" text... Is there any way to do this in the xml?

I even used android:autoLink="all". But when I used that, the text and button color changes and I dont want that.

Please Help.

like image 534
shankey Avatar asked Dec 09 '11 16:12

shankey


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

Can you use text-decoration to remove the underline from links?

To remove the underline from links, you can use the CSS text-decoration property.


2 Answers

TextView text=(TextView) findViewById(R.id.text);   

    String value = "<html> click to go <font color=#757b86><b><a href=\"http://www.google.com\">google</a></b></font> </html>";
Spannable spannedText = (Spannable)
                Html.fromHtml(value);
text.setMovementMethod(LinkMovementMethod.getInstance());

Spannable processedText = removeUnderlines(spannedText);
        text.setText(processedText);

here is your removeUnderlines()

public static Spannable 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);  
               }  
               return p_Text;  
          }  

also create class URLSpanNoUnderline.java

import co.questapp.quest.R;
import android.text.TextPaint;
import android.text.style.URLSpan;

public class URLSpanNoUnderline extends URLSpan {
    public URLSpanNoUnderline(String p_Url) {
        super(p_Url);
    }

    @Override
    public void updateDrawState(TextPaint p_DrawState) {
        super.updateDrawState(p_DrawState);
        p_DrawState.setUnderlineText(false);
        p_DrawState.setColor(R.color.info_text_color);
    }
}

using this line you can also change the color of that text p_DrawState.setColor(R.color.info_text_color);

like image 111
Sishin Avatar answered Oct 04 '22 03:10

Sishin


Kotlin has Textview extension for removing underline

fun TextView.removeLinksUnderline() {
    val spannable = SpannableString(text)
    for (urlSpan in spannable.getSpans(0, spannable.length, URLSpan::class.java)) {
        spannable.setSpan(object : URLSpan(urlSpan.url) {
            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.isUnderlineText = false
            }
        }, spannable.getSpanStart(urlSpan), spannable.getSpanEnd(urlSpan), 0)
    }
    text = spannable
}

Usage:

txtView.removeLinksUnderline() 
like image 23
Sirisha Avatar answered Oct 04 '22 03:10

Sirisha