Links in TextView Links in TextView android android

Links in TextView


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


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


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 .