5

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.

pablisco
  • 14,027
  • 4
  • 48
  • 70
shankey
  • 333
  • 2
  • 7
  • 18
  • There is an awesome answer for this here: http://stackoverflow.com/questions/4096851/remove-underline-from-links-in-textview-android – Warpzit Dec 09 '11 at 20:28
  • yeah before posting question I cheked that answer but I mentioned here already that I didin't use Linkify or anything else other than setMovementMethod(LinkMovementMethod.getInstance()); but thanks anyways. – shankey Dec 10 '11 at 07:08

5 Answers5

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

Nguyen Tan Dat
  • 3,780
  • 1
  • 23
  • 24
Sishin
  • 2,001
  • 1
  • 17
  • 22
  • I get grey links this way – CQM Jul 22 '14 at 19:24
  • p_DrawState.setColor(R.color.info_text_color); using this you can change the color of hyperlink text...... String value = " click to go google "; change color here for other color – Sishin Jul 24 '14 at 05:06
3

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() 
Sirisha
  • 423
  • 4
  • 12
1

after trying a lot finally i found the solution. I removed the link from string.xml and added this code in my java file.

Button btnGoogle = ( Button GoogleAlert.findViewById(
                R.id.btnGoogle );
            btnGoogle.setMovementMethod(
                LinkMovementMethod.getInstance() );
            btnGoogle.setOnClickListener( new OnClickListener()
            {
                @Override
                public void onClick( View v )
                {        
                    Uri uri = Uri.parse(
                        "http://www.google.com" );
                    Intent intent = new Intent( Intent.ACTION_VIEW, uri );
                    startActivity( intent );
                    buyAlert.dismiss();
                }
            } );

and it worked perfectly.

shankey
  • 333
  • 2
  • 7
  • 18
-1

You add style=\"text-decoration:none\" to your <a> tag.

For more information on how your string resources should be formatted, check out the String Resources Documentation.

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
-6
TextView text=(TextView) findViewById(R.id.text);
final String s = text.getText();
text.setText("");
text.setText(s);

that's all, folks ))

Valery Kulikov
  • 319
  • 1
  • 12
  • Sorry, doesn't work for me: TextView creditsTextView = (TextView) dialog.getCustomView().findViewById(R.id.text_credits); creditsTextView.setMovementMethod(LinkMovementMethod.getInstance()); final CharSequence s = creditsTextView.getText(); creditsTextView.setText(""); creditsTextView.setText(s); – Martin L. Dec 22 '16 at 15:11