Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link text in a TextView to open web URL

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.

like image 930
Jaison Brooks Avatar asked Nov 18 '25 13:11

Jaison Brooks


2 Answers

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

like image 59
Jaison Brooks Avatar answered Nov 21 '25 02:11

Jaison Brooks


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

like image 27
Sishin Avatar answered Nov 21 '25 01:11

Sishin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!