Links in TextView

61,048

Solution 1

Thank you for your help all.

I have managed to make this work, after I have found some examples in the android samples.

here is the code:

textView.setText(Html.fromHtml(
            "<b>text3:</b>  Text with a " +
            "<a href=\"http://www.google.com\">link</a> " +
            "created in the Java source code using HTML."));
textView.setMovementMethod(LinkMovementMethod.getInstance());

Hope this help others...

Solution 2

Getting links working from html is kind of tricky:

  1. Apply your text via xml android:text="@string/… or via setText() (see other answers)

  2. Use textView.setMovementMethod(LinkMovementMethod.getInstance()) to make links clickable (see other answers)

  3. Do NOT add android:autoLink="web" to you XML resource (section TextView), otherwise A-tags are not rendered correctly and are not clickable any longer.

Remark 1:
The OnClickListener can be helpful, if your TextView contains only one link and you want to trigger the navigation even if the user clicks aside your link, but inside the TextView.

Remark 2:
android:linksClickable="true" still does not work (as of Android 3.2), use p. 2 instead

Solution 3

Linkify is the class you must use to create links. BTW, what is the reason for not using Linkify?

You can linkify all text in your textview for actions like visiting a website or calling a phone number based on the schema. Android provides the easiest way to do it. Consider the below code.

TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);

For creating custom links, the same Linkify class provides various options. Google has published a blogpost on this .

Solution 4

I couldn't figure it out, but finally it started working when I did something like:

tvTermsOfUse.setText(Html.fromHtml(getString(R.string.tv_terms_of_use_html)));
Linkify.addLinks(tvTermsOfUse, Linkify.ALL);
tvTermsOfUse.setMovementMethod(LinkMovementMethod.getInstance());

Text view looks like:

<TextView
    android:id="@+id/tv_terms_of_use"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textAlignment="gravity"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="15sp" />

and string res:

<string name="tv_terms_of_use_html">
    <![CDATA[This is link to <a href="http://google.com/">Google</a>.]]>
</string>

Important part: The Linkify.addLinks has to be done before tvTermsOfUse.setMovementMethod, otherwise it won't work.

No other settings are necessary in XML.

It took me around hour to figure it out myself, hope it helps someone.

EDIT:

According to @rfellons comment

Thanks. Also for me works ... BUT only with

<uses-permission android:name="android.permission.INTERNET"/>

on Manifest.xml. – rfellons Sep 7 at 13:31

Make sure you check it as well.

Solution 5

Use

android:linksClickable="true"
android:autoLink="web"
Share:
61,048
Cata
Author by

Cata

Eager to learn and do new things. I consider myself an open minded person and I appreciate any feedback received because admitting your mistakes it's a good start to make improvements! ;)

Updated on July 09, 2022

Comments

  • Cata
    Cata almost 2 years

    I need to put a link in a TextView, I have a string that contains the tag <a href="link">Text for link</a> and some other text. The problem is that if I run the project I can see the text but it's not clickable. I tried with the <b> tag too to see if that works and it seems that it doesn't work too.

    How can I make this to work without the Linkify usage?

  • Jan
    Jan about 11 years
    Thanks for help , can you guide me how can i remove underline ?
  • Renan Franca
    Renan Franca almost 11 years
    Absoluty Right! Thanks for the time to write it down! PEOPLE! THE ANSWER IS HERE!!!!
  • iTurki
    iTurki almost 11 years
    This is the ONE. Thanks!
  • prcaen
    prcaen over 10 years
    This help me too! Thanks.
  • Konstantin Konopko
    Konstantin Konopko over 10 years
    It doesn't work if add link string from resource in TextView xml. It's only works with setText(Html.fromHtml(getResources().getString(R.string.STRI‌​NG_NAME))) for me
  • validcat
    validcat about 10 years
    Useful answer, help me a lot, thnks!
  • dbm
    dbm almost 10 years
    This solution ignores any phone number and email address links. I'm yet to find a solution which handles all kinds of links (web, phone, email, ...) in the same TextView.
  • Harsha
    Harsha over 9 years
    I have a Text View with json response data.that Response contains description and mail id and phone number how can i give click event for mail id and mobile number in that text view android.
  • marson
    marson over 9 years
    this is the easiest method out there and it worked fine
  • JPM
    JPM over 9 years
    I've done this but how do I know the user clicked it for tracking purposes?
  • Xebozone
    Xebozone over 8 years
    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()
  • Asaf
    Asaf almost 8 years
    Thanks! I tried all other answers in this and other similar questions and none worked. It also didn't work without the CDATA.
  • EpicPandaForce
    EpicPandaForce over 7 years
    This doesn't work for links like <a href="http://blah.com">http://blah.com</a>.
  • rfellons
    rfellons over 7 years
    Thanks. Also for me works ... BUT only with <uses-permission android:name="android.permission.INTERNET"/> on Manifest.xml.