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.
The above solutions didn't work for me, but the following did (and it seems a bit cleaner).
First, in the string resource, define your tag opening chevrons using the HTML entity encoding, i.e.:
and NOT:
In general, encode all the chevrons in the string like that. BTW, the link must start with
http://
Then (as suggested here) set this option on your TextView:
Finally, in code, do:
That's it, no regexes or other manual hacks required.
Here is very one line android code to make phone and url selectable from textView no matter what is string and what is data. You dont need to use any HTML tags for this.
Don't know if it's worth adding another answer, but just in case...
I had to hunt this down in a couple places but finally got this version of the code to work.
strings.xml:
myactivity.xml:
myactivty.java (in onCreate()):
This will create two clickable hyperlinks with the text
link text1
andlink text2
which redirect the user to google.i used this simply
makes the links clickable given here
I use the autolink to "auto underline" the text, but just made an "onClick" that manages it. (I ran into this problem myself)
Doesn't require any permissions, as you are passing the intent off to apps that manage those resources, (I.E. browser).
This was what worked for me. Good luck.
If you want to add HTML-like link, all you need to do is:
add a resource HTML-like string:
add your view to the layout with NO link-specific configuration at all:
add appropriate MovementMethod programmatically to your TextView:
That's it! And yes, having options like "autoLink" and "linksClickable" working on explicit links only (not wrapped into html tags) is very misleading to me too...