Android: textview hyperlink

AndroidHyperlinkTextview

Android Problem Overview


I know that if you put a link in a textview it will work but if I want to display for example:

google stackoverflow

and not the whole link(just the tag) How do i make those links clickable?

Android Solutions


Solution 1 - Android

You could have two separate TextViews and you could align them accordingly in your layout if needed:

	Text1.setText(
        Html.fromHtml(
            "<a href=\"http://www.google.com\">google</a> "));
	Text1.setMovementMethod(LinkMovementMethod.getInstance());
	
	Text2.setText(
            Html.fromHtml(
                "<a href=\"http://www.stackoverflow.com\">stackoverflow</a> "));
	Text2.setMovementMethod(LinkMovementMethod.getInstance());

Then if you want to strip the "link underline". Create a class:

public class URLSpanNoUnderline extends URLSpan {
    public URLSpanNoUnderline(String url) {
        super(url);
    }
    @Override public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
        }
}

Then add this method in your main Activity class where you have the 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);
}

And then just call this after you initialised the TextViews (in your onCreate):

stripUnderlines(Text1);
stripUnderlines(Text2);

Solution 2 - Android

TextView t2 = (TextView) findViewById(R.id.textviewidname);
t2.setMovementMethod(LinkMovementMethod.getInstance());

and

<string name="google_stackoverflow"><a href="https://stackoverflow.com/questions/9852184/android-textview-hyperlink?rq=1">google stack overflow</a></string>

The link is, "https://stackoverflow.com/questions/9852184/android-textview-hyperlink?rq=1"

and the tag is, "google stack overflow"

Define the first code block in your java and the second code block in your strings.xml file. Also, be sure to reference the id of the textView from your page layout in your java.

Solution 3 - Android

android:autoLink="web" simply works if you have full links in your HTML. The following will be highlighted in blue and clickable:

Solution 4 - Android

Very simple way to do this---

In your Activity--

 TextView tv = (TextView) findViewById(R.id.site);
 tv.setText(Html.fromHtml("<a href=http://www.stackoverflow.com> STACK OVERFLOW "));
 tv.setMovementMethod(LinkMovementMethod.getInstance());

Then you will get just the Tag, not the whole link..

Hope it will help you...

Solution 5 - Android

this should work.

TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());

and

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:id="@+id/text2"
 android:layout_centerInParent="true"
android:layout_marginTop="20dp"></TextView>

Solution 6 - Android

This is my working implementation

private void showMessage()
    {
    
        lblMessage.setText("");
    
        List<String> messages = db.getAllGCMMessages();
    
        for (int k = messages.size() - 1; k >= 0; --k)
         {
    
            String message  =  messages.get(k).toString();
            lblMessage.append(message + "\n\n");
    
         }
     Linkify.addLinks(lblMessage, Linkify.ALL);
  }

and to change color of hyperlinks , i editted my xml for textview -

 android:textColorLink="#69463d"

Solution 7 - Android

What about data binding?

@JvmStatic
@BindingAdapter("textHtml")
fun setHtml(textView: TextView, resource: String) {
    val html: Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml(resource, Html.FROM_HTML_MODE_COMPACT)
    } else {
        Html.fromHtml(resource)
    }

    textView.movementMethod = LinkMovementMethod.getInstance()
    textView.text = html
}

strings.xml

<string name="text_with_link">&lt;a href=%2$s>%1$s&lt;/a> </string>

in your layout.xml

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:textHtml="@{@string/text_with_link(model.title, model.url)}"
            tools:text="Some text" />

Where title and link in xml is a simple String

Also you can pass multiple arguments to data binding adapter

@JvmStatic
@BindingAdapter(value = ["textLink", "link"], requireAll = true)
fun setHtml(textView: TextView, textLink: String?, link: String?) {
    val resource = String.format(textView.context.getString(R.string.text_with_link, textLink, link))

    val html: Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml(resource, Html.FROM_HTML_MODE_COMPACT)
    } else {
        Html.fromHtml(resource)
    }

    textView.movementMethod = LinkMovementMethod.getInstance()
    textView.text = html
}

and in .xml pass arguments separately

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:link="@{model.url}"
            app:textLink="@{model.title}"
            tools:text="Some text" />

Solution 8 - Android

I hit on the same problem and finally find the working solution.

  1. in the string.xml file, define:

     <string name="textWithHtml">The URL link is &lt;a href="http://www.google.com">Google&lt;/a></string>
    

Replace the "<" less than character with HTML escaped character.

  1. In Java code:

     String text = v.getContext().getString(R.string.textWithHtml);
     textView.setText(Html.fromHtml(text));
     textView.setMovementMethod(LinkMovementMethod.getInstance());
    

And the TextBox will correctly display the text with clickable anchor link

Solution 9 - Android

Use

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="www.google.com" />

This flag > autolink="web"

controls whether links such as urls automatically found and converted to clickable links. The default value is "none", disabling this feature. Values: all, email, map, none, phone, web.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPew LabsView Question on Stackoverflow
Solution 1 - AndroidAndreiView Answer on Stackoverflow
Solution 2 - Androidepicness42View Answer on Stackoverflow
Solution 3 - AndroidM-RazaviView Answer on Stackoverflow
Solution 4 - AndroidHulkView Answer on Stackoverflow
Solution 5 - AndroidAnurag RamdasanView Answer on Stackoverflow
Solution 6 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 7 - AndroidAnet93View Answer on Stackoverflow
Solution 8 - AndroidIvanView Answer on Stackoverflow
Solution 9 - AndroidDan AlboteanuView Answer on Stackoverflow