3

I've spent over an hour looking at plenty of examples and none of it actually works for setting text in a TextView to link to a web URL.

Example code!

text8 = (TextView) findViewById(R.id.textView4);
text8.setMovementMethod(LinkMovementMethod.getInstance());

Strings.xml

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

main.xml

 <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:linksClickable="true"
        android:text="@string/urltext"
        android:textAppearance="?android:attr/textAppearanceMedium" />

Currently this code display the text as "Google" however its not hyperlinked and nothing happens upon clicking.

Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79

3 Answers3

8

I solved my problem simply by the following code.

  • Kept HTML-like string:

     <string name="urltext"><a href="https://www.google.com/">Google</a></string>
    
  • Made layout with NO link-specific configuration at all:

     <TextView
        android:id="@+id/link"
        android:text="@string/urltext" />`
    
  • Added the MovementMethod to the TextView:

     mLink = (TextView) findViewById(R.id.link);
     if (mLink != null) {
       mLink.setMovementMethod(LinkMovementMethod.getInstance());
     }
    

Now it allows me to click the hyperlinked text "Google" and now opens web browser.

This code was from vizZ answer at the following linked Question

Community
  • 1
  • 1
Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
1
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);
    }

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

Sishin
  • 2,001
  • 1
  • 17
  • 22
-1

Add CDATA to your string resource

Strings.xml

<string name="urltext"><![CDATA[<a href=\"http://www.google.com\">Google</a>]]></string>
Akexorcist
  • 2,287
  • 1
  • 16
  • 19