I have the following TextView defined:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:autoLink="web" android:id="@+id/infoTxtCredits"
android:layout_centerInParent="true"
android:linksClickable="true"></TextView>
where @string/txtCredits
is a string resource that contains <a href="some site">Link text</a>
.
Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me what I'm doing wrong? Do I have to set an onClickListener for the TextView in my activity for something as simple as this?
Looks like it has to do with the way I define my string resource. This does not work:
<string name="txtCredits"><a href="http://www.google.com">Google</a></string>
But this does:
<string name="txtCredits">www.google.com</string>
Which is a bummer because I would much rather show a text link than show the full URL.
Manage Linkify text Color Also
Use below code:
Buried in the API demos I found the solution to my problem:
Link.java:
I removed most of the attributes on my TextView to match what was in the demo.
That solved it. Pretty difficult to uncover and fix.
Important: Don't forget to remove
autoLink="web"
if you are callingsetMovementMethod()
.The easiest thing that worked for me is to use Linkify
and it will automatically detect the web urls from the text in the textview.
The accepted answer is correct, BUT it will mean that phone numbers, maps, email addresses, and regular links e.g.
http://google.com
without href tags will NO LONGER be clickable since you can't have autolink in the xml.The only complete solution to have EVERYTHING clickable that I have found is the following:
And the TextView should NOT have
android:autolink
. There's no need forandroid:linksClickable="true"
either; it's true by default.The reason you're having the problem is that it only tries to match "naked" addresses. things like "www.google.com" or "http://www.google.com".
Running your text through Html.fromHtml() should do the trick. You have to do it programatically, but it works.