Links in TextView
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?
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:
Apply your text via xml
android:text="@string/…
or viasetText()
(see other answers)Use
textView.setMovementMethod(LinkMovementMethod.getInstance())
to make links clickable (see other answers)Do NOT add
android:autoLink="web"
to you XML resource (sectionTextView
), 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 .