Links in TextView

AndroidTextviewHyperlink

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

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

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

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 > > > on Manifest.xml. – > rfellons Sep 7 at 13:31

Make sure you check it as well.

Solution 5 - Android

Use

android:linksClickable="true"
android:autoLink="web"

Solution 6 - Android

I can't reply to your answer for some reason; I just wanted to say that you can omit the textView.setText and just put it in a string resource, and set that using android:text. You just need to keep the textView.setMovementMethod(LinkMovementMethod.getInstance());; unfortunately android:linksClickable="true" by itself does not work.

Solution 7 - Android

The Solution : Linkify.addLinks(chatText,Linkify.ALL);

Solution 8 - Android

To add the links dynamically (fetched from the server), this works:

textView.setText(Html.fromHtml(
                "<a href=" + response.getLink()
                        + ">" + context.getString(R.string.link_from_server) + "</a> "));

and in XML add this:

android:linksClickable="true"

If your strings.xml has this:

<string name="link_from_server">Dynamic Link</string>

This will add "Dynamic Link" to your text view and if you touch on this, it will go the link provided by your server.

Solution 9 - Android

This works pretty correcty:(In textview properties,inside xml file)

android:autolink="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
QuestionCataView Question on Stackoverflow
Solution 1 - AndroidCataView Answer on Stackoverflow
Solution 2 - AndroidMichael BiermannView Answer on Stackoverflow
Solution 3 - AndroidGopinathView Answer on Stackoverflow
Solution 4 - AndroidDamian WalczakView Answer on Stackoverflow
Solution 5 - AndroidsonidaView Answer on Stackoverflow
Solution 6 - AndroidKevinView Answer on Stackoverflow
Solution 7 - Androidmanav patadiaView Answer on Stackoverflow
Solution 8 - AndroidcgrView Answer on Stackoverflow
Solution 9 - AndroidSaurabhView Answer on Stackoverflow