57

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Pew Labs
  • 809
  • 2
  • 8
  • 12
  • Check out this: http://stackoverflow.com/a/2746708/646391 – Emran Mar 24 '12 at 13:44
  • possible duplicate of [How do I make links in a TextView clickable?](http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable) – CAMOBAP Dec 11 '14 at 15:17
  • Several of the answers here call Html.fromHtml, and either don't use strings.xml for the hypertext or obfuscate the hypertext in the XML (e.g., "<" replacing "<" in the XML file). This unnecessarily complicates matters. AFAICT, the most correct answer would be to type the HTML anchor tag directly in strings.xml, make sure the autolink property for the TextView is not set, then use setMovementMethod to set the LinkMovementMethod. There are a couple of answers that already show how to do this. – monkey0506 Nov 06 '15 at 22:53
  • Possible duplicate of [Make a hyperlink textview in android](http://stackoverflow.com/questions/9290651/make-a-hyperlink-textview-in-android) – Ciro Santilli OurBigBook.com Feb 08 '16 at 14:21

9 Answers9

65

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);
TheIT
  • 11,919
  • 4
  • 64
  • 56
Andrei
  • 2,607
  • 1
  • 23
  • 26
  • 2
    Note: this will **not** work if you've set in your TextView's properties to autoLink some properties! If you have, you must call `textView.setAutoLinkMask(0);` before you call setText() – Xebozone Dec 21 '15 at 03:40
  • I don't recommend this solution if you need to translate your resources. – StarWind0 Jan 13 '22 at 23:13
61
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, "Android: textview hyperlink"

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.

Community
  • 1
  • 1
epicness42
  • 1,137
  • 11
  • 12
19

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

M-Razavi
  • 3,327
  • 2
  • 34
  • 46
17

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...

Hulk
  • 2,565
  • 16
  • 24
7

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>
Anurag Ramdasan
  • 4,189
  • 4
  • 31
  • 53
  • yes and for that you can include this in your string resource Google. – Anurag Ramdasan Mar 24 '12 at 14:16
  • 3
    Make sure that you do not have anything selected in autolink property. If there is anything selected in autolink property LinkMovementMethod.getInstance() does not work for that textview – Ram G. Dec 05 '13 at 00:35
3

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"
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
3

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" />
Anet93
  • 51
  • 1
2

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

Ivan
  • 417
  • 1
  • 4
  • 10
2

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.

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40