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?
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 usingandroid:text
. You just need to keep thetextView.setMovementMethod(LinkMovementMethod.getInstance());
; unfortunatelyandroid:linksClickable="true"
by itself does not work.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 yourTextView
contains only one link and you want to trigger the navigation even if the user clicks aside your link, but inside theTextView
.Remark 2:
android:linksClickable="true"
still does not work (as of Android 3.2), use p. 2 insteadThank 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:
Hope this help others...
The Solution :
Linkify.addLinks(chatText,Linkify.ALL);
To add the links dynamically (fetched from the server), this works:
and in XML add this:
If your strings.xml has this:
This will add "Dynamic Link" to your text view and if you touch on this, it will go the link provided by your server.
This works pretty correcty:(In textview properties,inside xml file)